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

support json tags in embedded structs #1396

Merged
merged 1 commit into from Nov 25, 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
6 changes: 5 additions & 1 deletion field_parser.go
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions parser.go
Expand Up @@ -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
Expand All @@ -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()
Expand Down
34 changes: 21 additions & 13 deletions testdata/simple/expected.json
Expand Up @@ -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"
}
}
}
}
Expand Down Expand Up @@ -722,12 +722,6 @@
"Data": {
"type": "integer"
},
"Err": {
"type": "integer"
},
"Status": {
"type": "boolean"
},
"cross": {
"$ref": "#/definitions/cross.Cross"
},
Expand All @@ -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"
}
}
},
Expand Down Expand Up @@ -805,4 +813,4 @@
}
}
}
}
}