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 4 commits
Commits
File filter

Filter by extension

Filter by extension

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

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

Expand Down Expand Up @@ -213,6 +212,16 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {
return fmt.Errorf("invalid type for field: %s", ps.field.Names[0])
}

if IsRefSchema(schema) {
oldSchema := schema
schema = &spec.Schema{}
defer func() {
if !reflect.ValueOf(*schema).IsZero() {
*oldSchema = *(schema.WithAllOf(*oldSchema))
ubogdan marked this conversation as resolved.
Show resolved Hide resolved
}
}()
}

if ps.field.Tag == nil {
if ps.field.Doc != nil {
schema.Description = strings.TrimSpace(ps.field.Doc.Text())
Expand Down Expand Up @@ -353,19 +362,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
Expand Up @@ -2506,7 +2506,11 @@ func Test(){
},
"test2": {
"description": "test2",
"$ref": "#/definitions/api.Child"
"allOf": [
{
"$ref": "#/definitions/api.Child"
}
]
}
}
}
Expand Down Expand Up @@ -2610,7 +2614,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
Expand Up @@ -135,6 +135,11 @@ func ignoreNameOverride(name string) bool {
return len(name) != 0 && name[0] == IgnoreNameOverridePrefix
}

// 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
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