From fb80159730126769dd580d2229ff68b1ab934ff2 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Mon, 21 Nov 2022 11:34:41 +0800 Subject: [PATCH 1/3] fix #1342 --- parser.go | 9 +++++++-- schema.go | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) 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) From 6031d3e76da06436f90cef636c4c2d8dcfdddd08 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Mon, 21 Nov 2022 13:26:18 +0800 Subject: [PATCH 2/3] fix #1342 --- parser.go | 18 +++++++++++++++--- schema.go | 7 ++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/parser.go b/parser.go index e7412fa06..f7b2a6bd6 100644 --- a/parser.go +++ b/parser.go @@ -1286,9 +1286,21 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st } } - err = ps.ComplementSchema(schema) - if err != nil { - return nil, nil, err + if IsRefSchema(schema) { + additionalSchema := *schema + err = ps.ComplementSchema(&additionalSchema) + if err != nil { + return nil, nil, err + } + if !reflect.DeepEqual(schema, &additionalSchema) { + additionalSchema.Ref = spec.Ref{} + schema = spec.ComposedSchema(*schema, additionalSchema) + } + } else { + err = ps.ComplementSchema(schema) + if err != nil { + return nil, nil, err + } } var tagRequired []string diff --git a/schema.go b/schema.go index 24303ca6e..c849f33cb 100644 --- a/schema.go +++ b/schema.go @@ -135,7 +135,7 @@ func ignoreNameOverride(name string) bool { return len(name) != 0 && name[0] == IgnoreNameOverridePrefix } -// IsComplexSchema whether a schema is complex and should be a ref schema +// IsComplexSchema whether a schema is complex and is supposed to be a ref schema func IsComplexSchema(schema *spec.Schema) bool { // a enum type should be complex if len(schema.Enum) > 0 { @@ -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) From 3d7eb8ac39b6fbe4a7e82c09ba82df390cd81e26 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Mon, 21 Nov 2022 14:41:31 +0800 Subject: [PATCH 3/3] Revert "fix #1342" This reverts commit 6031d3e76da06436f90cef636c4c2d8dcfdddd08. --- parser.go | 18 +++--------------- schema.go | 7 +------ 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/parser.go b/parser.go index f7b2a6bd6..e7412fa06 100644 --- a/parser.go +++ b/parser.go @@ -1286,21 +1286,9 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st } } - if IsRefSchema(schema) { - additionalSchema := *schema - err = ps.ComplementSchema(&additionalSchema) - if err != nil { - return nil, nil, err - } - if !reflect.DeepEqual(schema, &additionalSchema) { - additionalSchema.Ref = spec.Ref{} - schema = spec.ComposedSchema(*schema, additionalSchema) - } - } else { - err = ps.ComplementSchema(schema) - if err != nil { - return nil, nil, err - } + err = ps.ComplementSchema(schema) + if err != nil { + return nil, nil, err } var tagRequired []string diff --git a/schema.go b/schema.go index c849f33cb..24303ca6e 100644 --- a/schema.go +++ b/schema.go @@ -135,7 +135,7 @@ func ignoreNameOverride(name string) bool { return len(name) != 0 && name[0] == IgnoreNameOverridePrefix } -// IsComplexSchema whether a schema is complex and is supposed to be a ref schema +// 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 { @@ -156,11 +156,6 @@ 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)