From 80d522103ad6341cd26ecfc4c906df7a891943d6 Mon Sep 17 00:00:00 2001 From: Jakob Gillich Date: Fri, 25 Nov 2022 22:54:32 +0100 Subject: [PATCH] feat: support json tags in embedded structs (#1396) --- field_parser.go | 6 +++++- parser.go | 34 +++++++++++++++++----------------- testdata/simple/expected.json | 34 +++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/field_parser.go b/field_parser.go index 1a5cc6eaf..beee28ac3 100644 --- a/field_parser.go +++ b/field_parser.go @@ -43,7 +43,7 @@ func newTagBaseFieldParser(p *Parser, field *ast.Field) FieldParser { func (ps *tagBaseFieldParser) ShouldSkip() bool { // Skip non-exported fields. - if !ast.IsExported(ps.field.Names[0].Name) { + if ps.field.Names != nil && !ast.IsExported(ps.field.Names[0].Name) { return true } @@ -76,6 +76,10 @@ func (ps *tagBaseFieldParser) FieldName() (string, error) { } } + if ps.field.Names == nil { + return "", nil + } + switch ps.p.PropNamingStrategy { case SnakeCase: return toSnakeCase(ps.field.Names[0].Name), nil diff --git a/parser.go b/parser.go index 088ec5f09..e18e2e36d 100644 --- a/parser.go +++ b/parser.go @@ -1230,14 +1230,25 @@ func (parser *Parser) parseStruct(file *ast.File, fields *ast.FieldList) (*spec. } func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[string]spec.Schema, []string, error) { - if field.Names == nil { - if field.Tag != nil { - skip, ok := reflect.StructTag(strings.ReplaceAll(field.Tag.Value, "`", "")).Lookup("swaggerignore") - if ok && strings.EqualFold(skip, "true") { - return nil, nil, nil - } + if field.Tag != nil { + skip, ok := reflect.StructTag(strings.ReplaceAll(field.Tag.Value, "`", "")).Lookup("swaggerignore") + if ok && strings.EqualFold(skip, "true") { + return nil, nil, nil } + } + ps := parser.fieldParserFactory(parser, field) + + if ps.ShouldSkip() { + return nil, nil, nil + } + + fieldName, err := ps.FieldName() + if err != nil { + return nil, nil, err + } + + if fieldName == "" { typeName, err := getFieldType(file, field.Type) if err != nil { return nil, nil, err @@ -1260,20 +1271,9 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st return properties, schema.SchemaProps.Required, nil } - // for alias type of non-struct types ,such as array,map, etc. ignore field tag. return map[string]spec.Schema{typeName: *schema}, nil, nil - } - - ps := parser.fieldParserFactory(parser, field) - - if ps.ShouldSkip() { - return nil, nil, nil - } - fieldName, err := ps.FieldName() - if err != nil { - return nil, nil, err } schema, err := ps.CustomSchema() diff --git a/testdata/simple/expected.json b/testdata/simple/expected.json index 0904d3ffa..24ce9f696 100644 --- a/testdata/simple/expected.json +++ b/testdata/simple/expected.json @@ -215,17 +215,17 @@ } } }, - "404": { - "description": "Can not find ID", - "schema": { - "$ref": "#/definitions/web.APIError" - } - }, "403": { "description": "cross", "schema": { "$ref": "#/definitions/cross.Cross" } + }, + "404": { + "description": "Can not find ID", + "schema": { + "$ref": "#/definitions/web.APIError" + } } } } @@ -722,12 +722,6 @@ "Data": { "type": "integer" }, - "Err": { - "type": "integer" - }, - "Status": { - "type": "boolean" - }, "cross": { "$ref": "#/definitions/cross.Cross" }, @@ -736,6 +730,20 @@ "items": { "$ref": "#/definitions/cross.Cross" } + }, + "rev_value_base": { + "$ref": "#/definitions/web.RevValueBase" + } + } + }, + "web.RevValueBase": { + "type": "object", + "properties": { + "Err": { + "type": "integer" + }, + "Status": { + "type": "boolean" } } }, @@ -805,4 +813,4 @@ } } } -} +} \ No newline at end of file