diff --git a/parser.go b/parser.go index e9d44e00e..e7412fa06 100644 --- a/parser.go +++ b/parser.go @@ -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 diff --git a/schema.go b/schema.go index 5949113fb..24303ca6e 100644 --- a/schema.go +++ b/schema.go @@ -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)