Skip to content

Commit

Permalink
Add ability to set 'swaggerignore' on embedded fields (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffenix113 authored Aug 27, 2020
1 parent 1889a3a commit 6a59a20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,13 @@ func (sf *structField) toStandardSchema() *spec.Schema {

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
}
}

typeName, err := getFieldType(field.Type)
if err != nil {
return nil, nil, err
Expand Down
43 changes: 43 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2410,3 +2410,46 @@ func TestParseJSONFieldString(t *testing.T) {
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}

func TestParseSwaggerignoreForEmbedded(t *testing.T) {
src := `
package main
type Child struct {
ChildName string
}//@name Student
type Parent struct {
Name string
Child ` + "`swaggerignore:\"true\"`" + `
}//@name Teacher
// @Param request body Parent true "query params"
// @Success 200 {object} Parent
// @Router /test [get]
func Fun() {
}
`
f, err := goparser.ParseFile(token.NewFileSet(), "", src, goparser.ParseComments)
assert.NoError(t, err)

p := New()
p.packages.CollectAstFile("api", "api/api.go", f)
p.packages.ParseTypes()
err = p.ParseRouterAPIInfo("", f)
assert.NoError(t, err)

assert.NoError(t, err)
teacher, ok := p.swagger.Definitions["Teacher"]
assert.True(t, ok)

name, ok := teacher.Properties["name"]
assert.True(t, ok)
assert.Len(t, name.Type, 1)
assert.Equal(t, "string", name.Type[0])

childName, ok := teacher.Properties["childName"]
assert.False(t, ok)
assert.Empty(t, childName)
}

0 comments on commit 6a59a20

Please sign in to comment.