From bdbc702bd8623514bddca954f0d9e7889daa3daa Mon Sep 17 00:00:00 2001 From: chengjin Date: Fri, 20 Dec 2019 19:03:05 +0800 Subject: [PATCH] support []map --- parser.go | 14 ++++++++++++++ parser_test.go | 15 +++++++++++++-- property.go | 12 ++++++------ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/parser.go b/parser.go index 535cca2bf..b3f42f002 100644 --- a/parser.go +++ b/parser.go @@ -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 { diff --git a/parser_test.go b/parser_test.go index 953e06e52..b610275e9 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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)) } @@ -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 @@ -2532,6 +2533,16 @@ func Test(){ "items": { "$ref": "#/definitions/api.Child" } + }, + "test9": { + "description": "test9", + "type": "array", + "items": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } } } } diff --git a/property.go b/property.go index 198e8852d..999aec8f6 100644 --- a/property.go +++ b/property.go @@ -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 } @@ -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) }