Skip to content

Commit

Permalink
Add read only test; Remove redundant code; Improve test by using cust…
Browse files Browse the repository at this point in the history
…om type for value of map.
  • Loading branch information
lvandenbrink committed Sep 23, 2019
1 parent 4fe50f7 commit 93f48ba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 57 deletions.
57 changes: 2 additions & 55 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,60 +931,6 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
}
}
} else if astTypeMap, ok := field.Type.(*ast.MapType); ok { // if map
props := make(map[string]spec.Schema)
// parse key type of map
if expr, ok := astTypeMap.Key.(*ast.StructType); ok {
for _, field := range expr.Fields.List {
var fieldProps map[string]spec.Schema
fieldProps, _, err = parser.parseStructField(pkgName, field)
if err != nil {
return properties, nil, 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,
},
},
},
}}
}
// parse value type of map
if expr, ok := astTypeMap.Value.(*ast.StructType); ok {
for _, field := range expr.Fields.List {
var fieldProps map[string]spec.Schema
fieldProps, _, err = parser.parseStructField(pkgName, field)
if err != nil {
return properties, nil, 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,
},
},
},
}}
}

itemSchema, err := parser.parseTypeExpr(pkgName, "", astTypeMap.Value)
if err != nil {
return properties, nil, err
Expand All @@ -1011,7 +957,8 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: structField.exampleValue,
Example: structField.exampleValue,
ReadOnly: structField.readOnly,
},
VendorExtensible: spec.VendorExtensible{
Extensions: structField.extensions,
Expand Down
6 changes: 5 additions & 1 deletion testdata/composition/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ type FooBarPointer struct {
type BarMap map[string]Bar

type FooBarMap struct {
Field3 map[string]Bar
Field3 map[string]MapValue
}

type MapValue struct {
Field4 string
}

// @Description get Foo
Expand Down
10 changes: 9 additions & 1 deletion testdata/composition/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
"additionalProperties": {
"type": "object",
"properties": {
"field2": {
"field4": {
"type": "string"
}
}
Expand All @@ -200,6 +200,14 @@
"type": "string"
}
}
},
"api.MapValue": {
"type": "object",
"properties": {
"field4": {
"type": "string"
}
}
}
}
}

0 comments on commit 93f48ba

Please sign in to comment.