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

fix: support anonymous nested structure slice parsing #462

Merged
merged 6 commits into from
Jul 18, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,38 @@ func (parser *Parser) parseStruct(pkgName string, field *ast.Field) (map[string]
},
},
}
} else { // standard type in array
} else if structField.arrayType == "object" {
// Anonymous struct
if astTypeArray, ok := field.Type.(*ast.ArrayType); ok { // if array
props := make(map[string]spec.Schema)
if expr, ok := astTypeArray.Elt.(*ast.StructType); ok {
for _, field := range expr.Fields.List {
var fieldProps map[string]spec.Schema
fieldProps, err = parser.parseStruct(pkgName, field)
if err != nil {
return properties, err
}
for k, v := range fieldProps {
props[k] = v
}
}
properties[structField.name] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{structField.schemaType},
Description: desc,
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: props,
},
},
},
}}
}
}
} else {
// standard type in array
required := make([]string, 0)
if structField.isRequired {
required = append(required, structField.name)
Expand Down
59 changes: 59 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,65 @@ func TestParseComposition(t *testing.T) {
assert.Equal(t, string(expected), string(b))
}

func TestParser_ParseStuctArrayObject(t *testing.T) {
src := `
package api

type Response struct {
Code int
Data []struct{
Field1 uint
Field2 string
}
}

// @Success 200 {object} Response
// @Router /api/{id} [get]
func Test(){
}
`
expected := `{
"api.Response": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"type": "integer"
},
"field2": {
"type": "string"
}
}
}
}
}
}
}`
f, err := goparser.ParseFile(token.NewFileSet(), "", src, goparser.ParseComments)
assert.NoError(t, err)

p := New()
p.ParseType(f)
err = p.ParseRouterAPIInfo("", f)
assert.NoError(t, err)

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

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

}

func TestParser_ParseRouterApiInfoErr(t *testing.T) {
src := `
package test
Expand Down
4 changes: 3 additions & 1 deletion property.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func getPropertyName(expr ast.Expr, parser *Parser) (propertyName, error) {
}

if astTypeArray, ok := expr.(*ast.ArrayType); ok { // if array
if _, ok := astTypeArray.Elt.(*ast.StructType); ok {
return propertyName{SchemaType: "array", ArrayType: "object"}, nil
}
return getArrayPropertyName(astTypeArray, parser), nil
}

Expand All @@ -105,7 +108,6 @@ func getPropertyName(expr ast.Expr, parser *Parser) (propertyName, error) {
if _, ok := expr.(*ast.InterfaceType); ok { // if interface{}
return propertyName{SchemaType: "object", ArrayType: "object"}, nil
}

return propertyName{}, errors.New("not supported" + fmt.Sprint(expr))
}

Expand Down
17 changes: 17 additions & 0 deletions property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ func TestGetPropertyNameArrayStarExprSelector(t *testing.T) {
assert.Equal(t, expected, propertyName)
}

func TestGetPropertyNameArrayStructType(t *testing.T) {
input := &ast.ArrayType{
Lbrack: 1111,
Len: nil,
Elt: &ast.StructType{},
}
expected := propertyName{
"array",
"object",
"",
}

propertyName, err := getPropertyName(input, New())
assert.NoError(t, err)
assert.Equal(t, expected, propertyName)
}

func TestGetPropertyNameMap(t *testing.T) {
input := &ast.MapType{
Key: &ast.Ident{
Expand Down