Skip to content

Commit 1a7e648

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>
1 parent 4b94238 commit 1a7e648

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

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: 9 additions & 4 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() {

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)