Skip to content

Commit

Permalink
fix: wrongly shared schema by fields (#1389)
Browse files Browse the repository at this point in the history
* fix #1342

* fix #1342

* Revert "fix #1342"

This reverts commit 6031d3e.
  • Loading branch information
sdghchj committed Nov 22, 2022
1 parent e6723fe commit 8117f4c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions parser.go
Expand Up @@ -964,8 +964,13 @@ func (parser *Parser) getTypeSchema(typeName string, file *ast.File, ref bool) (
}
}

if ref && (len(schema.Schema.Type) > 0 && schema.Schema.Type[0] == OBJECT || len(schema.Enum) > 0) {
return parser.getRefTypeSchema(typeSpecDef, schema), nil
if ref {
if IsComplexSchema(schema.Schema) {
return parser.getRefTypeSchema(typeSpecDef, schema), nil
}
// if it is a simple schema, just return a copy
newSchema := *schema.Schema
return &newSchema, nil
}

return schema.Schema, nil
Expand Down
21 changes: 21 additions & 0 deletions schema.go
Expand Up @@ -135,6 +135,27 @@ func ignoreNameOverride(name string) bool {
return len(name) != 0 && name[0] == IgnoreNameOverridePrefix
}

// IsComplexSchema whether a schema is complex and should be a ref schema
func IsComplexSchema(schema *spec.Schema) bool {
// a enum type should be complex
if len(schema.Enum) > 0 {
return true
}

// a deep array type is complex, how to determine deep? here more than 2 ,for example: [][]object,[][][]int
if len(schema.Type) > 2 {
return true
}

//Object included, such as Object or []Object
for _, st := range schema.Type {
if st == OBJECT {
return true
}
}
return false
}

// RefSchema build a reference schema.
func RefSchema(refType string) *spec.Schema {
return spec.RefSchema("#/definitions/" + refType)
Expand Down

0 comments on commit 8117f4c

Please sign in to comment.