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

support []map #586

Merged
merged 1 commit into from Dec 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions parser.go
Expand Up @@ -975,6 +975,20 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
ReadOnly: structField.readOnly,
},
}
} else {
schema, _ := parser.parseTypeExpr(pkgName, "", astTypeArray.Elt)
properties[structField.name] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{structField.schemaType},
Description: structField.desc,
Items: &spec.SchemaOrArray{
Schema: schema,
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
ReadOnly: structField.readOnly,
},
}
}
}
} else {
Expand Down
15 changes: 13 additions & 2 deletions parser_test.go
Expand Up @@ -253,8 +253,8 @@ func TestGetAllGoFileInfo(t *testing.T) {
err := p.getAllGoFileInfo(searchDir)

assert.NoError(t, err)
assert.NotEmpty(t, p.files["testdata/pet/main.go"])
assert.NotEmpty(t, p.files["testdata/pet/web/handler.go"])
assert.NotEmpty(t, p.files[filepath.Join("testdata", "pet", "main.go")])
assert.NotEmpty(t, p.files[filepath.Join("testdata", "pet", "web", "handler.go")])
assert.Equal(t, 2, len(p.files))
}

Expand Down Expand Up @@ -2456,6 +2456,7 @@ type Parent struct {
Test6 MyMapType //test6
Test7 []Child //test7
Test8 []*Child //test8
Test9 []map[string]string //test9
}

// @Success 200 {object} Parent
Expand Down Expand Up @@ -2532,6 +2533,16 @@ func Test(){
"items": {
"$ref": "#/definitions/api.Child"
}
},
"test9": {
"description": "test9",
"type": "array",
"items": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions property.go
Expand Up @@ -104,9 +104,6 @@ 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 @@ -125,10 +122,13 @@ func getPropertyName(expr ast.Expr, parser *Parser) (propertyName, error) {
}

func getArrayPropertyName(astTypeArray *ast.ArrayType, parser *Parser) propertyName {
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.SelectorExpr); ok {
if _, ok := astTypeArray.Elt.(*ast.StructType); ok {
return propertyName{SchemaType: "array", ArrayType: "object"}
} else if _, ok := astTypeArray.Elt.(*ast.MapType); ok {
return propertyName{SchemaType: "array", ArrayType: "object"}
} else if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArrayExpr, parser, newArrayProperty)
}
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
} else if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the code seems like a little messy. It should be refactored in a good way in the future.

if astTypeArraySel, ok := astTypeArrayExpr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArraySel, parser, newArrayProperty)
}
Expand Down