Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add properties to $ref #1391

Merged
merged 6 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"
"unicode"

"github.com/go-openapi/jsonreference"
"github.com/go-openapi/spec"
)

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
ubogdan marked this conversation as resolved.
Show resolved Hide resolved
}

defaultTagValue := ps.tag.Get(defaultTag)
if defaultTagValue != "" {
value, err := defineType(field.schemaType, defaultTagValue)
Expand Down
12 changes: 10 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2497,7 +2497,11 @@ func Test(){
},
"test2": {
"description": "test2",
"$ref": "#/definitions/api.Child"
"allOf": [
{
"$ref": "#/definitions/api.Child"
}
]
}
}
}
Expand Down Expand Up @@ -2601,7 +2605,11 @@ func Test(){
},
"test6": {
"description": "test6",
"$ref": "#/definitions/api.MyMapType"
"allOf": [
{
"$ref": "#/definitions/api.MyMapType"
}
]
},
"test7": {
"description": "test7",
Expand Down
5 changes: 5 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 55 additions & 11 deletions testdata/generics_nested/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -266,7 +278,11 @@
"properties": {
"items": {
"description": "Items from the list response",
"$ref": "#/definitions/types.Post"
"allOf": [
{
"$ref": "#/definitions/types.Post"
}
]
}
}
},
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down