Skip to content

Commit 57152d0

Browse files
committed
fix: handle swagger:type array by falling through to underlying type resolution
When swagger:type is set to a value not handled by swaggerSchemaForType (e.g., "array"), the function returns an error. Previously this error was silently ignored and the function returned nil, losing all type info. Now, when swaggerSchemaForType returns an error, the code falls through to buildFromType with the underlying type, producing the correct schema. For example, swagger:type array on type StringSlice []string now produces {type: "array", items: {type: "string"}} with the field's description preserved, instead of a $ref that drops the description. * fixes #10 Signed-off-by: Kevin Doan <kevin.doan@ory.sh> Signed-off-by: KT-Doan <kevin.doan@ory.sh>
1 parent 4b94238 commit 57152d0

4 files changed

Lines changed: 61 additions & 8 deletions

File tree

application_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestMain(m *testing.M) {
5151
func TestApplication_LoadCode(t *testing.T) {
5252
sctx := loadClassificationPkgsCtx(t)
5353
require.NotNil(t, sctx)
54-
require.Len(t, sctx.app.Models, 39)
54+
require.Len(t, sctx.app.Models, 40)
5555
require.Len(t, sctx.app.Meta, 1)
5656
require.Len(t, sctx.app.Routes, 7)
5757
require.Empty(t, sctx.app.Operations)

fixtures/goparsing/classification/models/nomodel.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,13 @@ type NamedMapOfStoreOrderSlices GenericMap[string, GenericSlice[StoreOrder]]
799799
//
800800
// End of models related to named types with type arguments
801801
//
802+
803+
// SomeStringSlice is a named slice type with swagger:type array.
804+
// swagger:type array
805+
type SomeStringSlice []string
806+
807+
// swagger:model namedWithArrayType
808+
type NamedWithArrayType struct {
809+
// Tags for this item.
810+
Tags SomeStringSlice `json:"tags"`
811+
}

schema.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,15 @@ func (s *schemaBuilder) buildNamedType(titpe *types.Named, tgt swaggerTypable) e
429429
cmt = new(ast.CommentGroup)
430430
}
431431

432-
if typeName, ok := typeName(cmt); ok {
433-
_ = swaggerSchemaForType(typeName, tgt)
434-
435-
return nil
432+
if tn, ok := typeName(cmt); ok {
433+
if err := swaggerSchemaForType(tn, tgt); err == nil {
434+
return nil
435+
}
436+
// For unsupported swagger:type values (e.g., "array"), fall through
437+
// to underlying type resolution so the full schema (including items
438+
// for slices) is properly built. Build directly from the underlying
439+
// type to bypass the named-type $ref creation.
440+
return s.buildFromType(titpe.Underlying(), tgt)
436441
}
437442

438443
if s.decl.Spec.Assign.IsValid() {
@@ -559,9 +564,12 @@ func (s *schemaBuilder) buildNamedStruct(tio *types.TypeName, cmt *ast.CommentGr
559564
return nil
560565
}
561566

562-
if typeName, ok := typeName(cmt); ok {
563-
_ = swaggerSchemaForType(typeName, tgt)
564-
return nil
567+
if tn, ok := typeName(cmt); ok {
568+
if err := swaggerSchemaForType(tn, tgt); err == nil {
569+
return nil
570+
}
571+
// For unsupported swagger:type values, fall through to makeRef
572+
// rather than silently returning an empty schema.
565573
}
566574

567575
return s.makeRef(decl, tgt)
@@ -583,6 +591,14 @@ func (s *schemaBuilder) buildNamedArray(tio *types.TypeName, cmt *ast.CommentGro
583591
tgt.Items().Typed("string", sfnm)
584592
return nil
585593
}
594+
// When swagger:type is set to an unsupported value (e.g., "array"),
595+
// skip the $ref and inline the array schema with proper items type.
596+
if tn, ok := typeName(cmt); ok {
597+
if err := swaggerSchemaForType(tn, tgt); err != nil {
598+
return s.buildFromType(elem, tgt.Items())
599+
}
600+
return nil
601+
}
586602
if decl, ok := s.ctx.FindModel(tio.Pkg().Path(), tio.Name()); ok {
587603
return s.makeRef(decl, tgt)
588604
}
@@ -600,6 +616,16 @@ func (s *schemaBuilder) buildNamedSlice(tio *types.TypeName, cmt *ast.CommentGro
600616
tgt.Items().Typed("string", sfnm)
601617
return nil
602618
}
619+
// When swagger:type is set to an unsupported value (e.g., "array"),
620+
// skip the $ref and inline the slice schema with proper items type.
621+
// This preserves the field's description that would be lost with $ref.
622+
if tn, ok := typeName(cmt); ok {
623+
if err := swaggerSchemaForType(tn, tgt); err != nil {
624+
// Unsupported type name (e.g., "array") — build inline from element type.
625+
return s.buildFromType(elem, tgt.Items())
626+
}
627+
return nil
628+
}
603629
if decl, ok := s.ctx.FindModel(tio.Pkg().Path(), tio.Name()); ok {
604630
return s.makeRef(decl, tgt)
605631
}

schema_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,3 +2582,20 @@ func TestSetEnumDoesNotPanic(t *testing.T) {
25822582

25832583
require.NoError(t, err)
25842584
}
2585+
2586+
func TestSwaggerTypeNamedArray(t *testing.T) {
2587+
sctx := loadClassificationPkgsCtx(t)
2588+
decl := getClassificationModel(sctx, "NamedWithArrayType")
2589+
require.NotNil(t, decl)
2590+
prs := &schemaBuilder{
2591+
ctx: sctx,
2592+
decl: decl,
2593+
}
2594+
models := make(map[string]spec.Schema)
2595+
require.NoError(t, prs.Build(models))
2596+
schema := models["namedWithArrayType"]
2597+
2598+
// swagger:type array on a named []string type should produce
2599+
// an inlined array with string items, not a $ref.
2600+
assertArrayProperty(t, &schema, "string", "tags", "", "Tags")
2601+
}

0 commit comments

Comments
 (0)