Skip to content

Commit

Permalink
feat: support json tags in embedded structs (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgillich authored Nov 25, 2022
1 parent 362d05b commit 80d5221
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
6 changes: 5 additions & 1 deletion field_parser.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@
}
}
}
}
}

0 comments on commit 80d5221

Please sign in to comment.