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

fix: wrongly shared schema by fields #1389

Merged
merged 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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