diff --git a/parser.go b/parser.go index 4c786e0cc..c5eb92433 100644 --- a/parser.go +++ b/parser.go @@ -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 diff --git a/parser_test.go b/parser_test.go index 272467f43..1b187089b 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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) +}