Skip to content

Commit

Permalink
add properties to $ref (#1391)
Browse files Browse the repository at this point in the history
* add properties to $ref
  • Loading branch information
sdghchj authored Nov 22, 2022
1 parent e5d507d commit 29d3d30
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 27 deletions.
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
}

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

0 comments on commit 29d3d30

Please sign in to comment.