Skip to content

Commit

Permalink
feat: support array of maps (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdghchj authored and easonlin404 committed Dec 23, 2019
1 parent eb4805c commit 0e5dd7d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
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 {
if astTypeArraySel, ok := astTypeArrayExpr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArraySel, parser, newArrayProperty)
}
Expand Down

0 comments on commit 0e5dd7d

Please sign in to comment.