Skip to content

Commit

Permalink
feat: embeded struct not parsed (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored and easonlin404 committed Jul 19, 2019
1 parent 72f30f8 commit be88a64
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,13 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field) (map
Printf("Composite field type of '%T' is unhandle by parser. Skipping", ftype)
return properties, []string{}, nil
}
case *ast.SelectorExpr:
packageX, ok := ftype.X.(*ast.Ident)
if !ok {
Printf("Composite field type of '%T' is unhandle by parser. Skipping", ftype)
return properties, []string{}, nil
}
fullTypeName = fmt.Sprintf("%s.%s", packageX.Name, ftype.Sel.Name)
default:
Printf("Field type of '%T' is unsupported. Skipping", ftype)
return properties, []string{}, nil
Expand Down
69 changes: 69 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2309,6 +2309,75 @@ func Test(){

}

func TestParser_ParseEmbededStruct(t *testing.T) {
src := `
package api
type Response struct {
rest.ResponseWrapper
}
// @Success 200 {object} Response
// @Router /api/{id} [get]
func Test(){
}
`
restsrc := `
package rest
type ResponseWrapper struct {
Status string
Code int
Messages []string
Result interface{}
}
`
expected := `{
"api.Response": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"messages": {
"type": "array",
"items": {
"type": "string"
}
},
"result": {
"type": "object"
},
"status": {
"type": "string"
}
}
}
}`
parser := New()
parser.ParseDependency = true

f, err := goparser.ParseFile(token.NewFileSet(), "", src, goparser.ParseComments)
assert.NoError(t, err)
parser.ParseType(f)

f2, err := goparser.ParseFile(token.NewFileSet(), "", restsrc, goparser.ParseComments)
assert.NoError(t, err)
parser.ParseType(f2)

err = parser.ParseRouterAPIInfo("", f)
assert.NoError(t, err)

typeSpec := parser.TypeDefinitions["api"]["Response"]
err = parser.ParseDefinition("api", typeSpec.Name.Name, typeSpec)
assert.NoError(t, err)

out, err := json.MarshalIndent(parser.swagger.Definitions, "", " ")
assert.NoError(t, err)
assert.Equal(t, expected, string(out))

}

func TestParser_ParseRouterApiInfoErr(t *testing.T) {
src := `
package test
Expand Down

0 comments on commit be88a64

Please sign in to comment.