diff --git a/field_parser.go b/field_parser.go index 66f85427f..1a5cc6eaf 100644 --- a/field_parser.go +++ b/field_parser.go @@ -10,7 +10,6 @@ import ( "sync" "unicode" - "github.com/go-openapi/jsonreference" "github.com/go-openapi/spec" ) @@ -207,12 +206,30 @@ func splitNotWrapped(s string, sep rune) []string { return result } +// ComplementSchema complement schema with field properties func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error { types := ps.p.GetSchemaTypePath(schema, 2) if len(types) == 0 { return fmt.Errorf("invalid type for field: %s", ps.field.Names[0]) } + if IsRefSchema(schema) { + var newSchema = spec.Schema{} + err := ps.complementSchema(&newSchema, types) + if err != nil { + return err + } + if !reflect.ValueOf(newSchema).IsZero() { + *schema = *(newSchema.WithAllOf(*schema)) + } + return nil + } + + return ps.complementSchema(schema, types) +} + +// complementSchema complement schema with field properties +func (ps *tagBaseFieldParser) complementSchema(schema *spec.Schema, types []string) error { if ps.field.Tag == nil { if ps.field.Doc != nil { schema.Description = strings.TrimSpace(ps.field.Doc.Text()) @@ -353,19 +370,6 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error { schema.ReadOnly = ps.tag.Get(readOnlyTag) == "true" - if !reflect.ValueOf(schema.Ref).IsZero() && schema.ReadOnly { - schema.AllOf = []spec.Schema{*spec.RefSchema(schema.Ref.String())} - schema.Ref = spec.Ref{ - Ref: jsonreference.Ref{ - HasFullURL: false, - HasURLPathOnly: false, - HasFragmentOnly: false, - HasFileScheme: false, - HasFullFilePath: false, - }, - } // clear out existing ref - } - defaultTagValue := ps.tag.Get(defaultTag) if defaultTagValue != "" { value, err := defineType(field.schemaType, defaultTagValue) diff --git a/parser_test.go b/parser_test.go index a87b8bfea..d7735c790 100644 --- a/parser_test.go +++ b/parser_test.go @@ -2497,7 +2497,11 @@ func Test(){ }, "test2": { "description": "test2", - "$ref": "#/definitions/api.Child" + "allOf": [ + { + "$ref": "#/definitions/api.Child" + } + ] } } } @@ -2601,7 +2605,11 @@ func Test(){ }, "test6": { "description": "test6", - "$ref": "#/definitions/api.MyMapType" + "allOf": [ + { + "$ref": "#/definitions/api.MyMapType" + } + ] }, "test7": { "description": "test7", diff --git a/schema.go b/schema.go index 24303ca6e..ba92ac663 100644 --- a/schema.go +++ b/schema.go @@ -156,6 +156,11 @@ func IsComplexSchema(schema *spec.Schema) bool { return false } +// IsRefSchema whether a schema is a reference schema. +func IsRefSchema(schema *spec.Schema) bool { + return schema.Ref.Ref.GetURL() != nil +} + // RefSchema build a reference schema. func RefSchema(refType string) *spec.Schema { return spec.RefSchema("#/definitions/" + refType) diff --git a/testdata/generics_nested/expected.json b/testdata/generics_nested/expected.json index a2d005505..850fc5ae4 100644 --- a/testdata/generics_nested/expected.json +++ b/testdata/generics_nested/expected.json @@ -188,7 +188,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/types.Post" + "allOf": [ + { + "$ref": "#/definitions/types.Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing", @@ -207,7 +211,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/types.Post" + "allOf": [ + { + "$ref": "#/definitions/types.Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing", @@ -223,7 +231,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/types.Post" + "allOf": [ + { + "$ref": "#/definitions/types.Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing", @@ -266,7 +278,11 @@ "properties": { "items": { "description": "Items from the list response", - "$ref": "#/definitions/types.Post" + "allOf": [ + { + "$ref": "#/definitions/types.Post" + } + ] } } }, @@ -275,7 +291,11 @@ "properties": { "items": { "description": "Items from the list response", - "$ref": "#/definitions/web.GenericInnerType-array_types_Post" + "allOf": [ + { + "$ref": "#/definitions/web.GenericInnerType-array_types_Post" + } + ] }, "status": { "description": "Status of some other stuff", @@ -288,7 +308,11 @@ "properties": { "items": { "description": "Items from the list response", - "$ref": "#/definitions/web.GenericInnerType-types_Post" + "allOf": [ + { + "$ref": "#/definitions/web.GenericInnerType-types_Post" + } + ] }, "status": { "description": "Status of some other stuff", @@ -439,7 +463,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/types.Post" + "allOf": [ + { + "$ref": "#/definitions/types.Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing", @@ -459,7 +487,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/types.Post" + "allOf": [ + { + "$ref": "#/definitions/types.Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing", @@ -479,7 +511,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/types.Post" + "allOf": [ + { + "$ref": "#/definitions/types.Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing", @@ -499,7 +535,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/web.GenericInnerType-array_types_Post" + "allOf": [ + { + "$ref": "#/definitions/web.GenericInnerType-array_types_Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing", @@ -522,7 +562,11 @@ "properties": { "itemOne": { "description": "ItemsOne is the first thing", - "$ref": "#/definitions/web.GenericInnerType-types_Post" + "allOf": [ + { + "$ref": "#/definitions/web.GenericInnerType-types_Post" + } + ] }, "itemsTwo": { "description": "ItemsTwo is the second thing",