Skip to content

Commit

Permalink
add support for map nested in struct (#521)
Browse files Browse the repository at this point in the history
* add support for map nested in struct
  • Loading branch information
Laurens van den Brink authored and ubogdan committed Nov 22, 2019
1 parent b564023 commit 68ab45d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
46 changes: 46 additions & 0 deletions parser.go
Expand Up @@ -935,6 +935,52 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
},
}
}
} else if astTypeMap, ok := field.Type.(*ast.MapType); ok { // if map
_, err := parser.parseTypeExpr(pkgName, "", astTypeMap.Value)
if err != nil {
return properties, nil, err
}

fullTypeName, err := getFieldType(astTypeMap.Value)
if err != nil {
return properties, nil, err
}
mapValueScheme := &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + fullTypeName),
},
},
}

required := make([]string, 0)
if structField.isRequired {
required = append(required, structField.name)
}
properties[structField.name] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{structField.schemaType},
Description: desc,
Format: structField.formatType,
Required: required,
Maximum: structField.maximum,
Minimum: structField.minimum,
MaxLength: structField.maxLength,
MinLength: structField.minLength,
Enum: structField.enums,
Default: structField.defaultValue,
AdditionalProperties: &spec.SchemaOrBool{
Schema: mapValueScheme,
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: structField.exampleValue,
ReadOnly: structField.readOnly,
},
VendorExtensible: spec.VendorExtensible{
Extensions: structField.extensions,
},
}
} else {
required := make([]string, 0)
if structField.isRequired {
Expand Down
19 changes: 19 additions & 0 deletions testdata/composition/api/api.go
Expand Up @@ -25,6 +25,14 @@ type FooBarPointer struct {

type BarMap map[string]Bar

type FooBarMap struct {
Field3 map[string]MapValue
}

type MapValue struct {
Field4 string
}

// @Description get Foo
// @ID get-foo
// @Accept json
Expand Down Expand Up @@ -79,3 +87,14 @@ func GetBarMap(c *gin.Context) {
//write your code
var _ = BarMap{}
}

// @Description get FoorBarMap
// @ID get-foo-bar-map
// @Accept json
// @Produce json
// @Success 200 {object} api.FooBarMap
// @Router /testapi/get-foobarmap [get]
func GetFooBarMap(c *gin.Context) {
//write your code
var _ = FooBarMap{}
}
39 changes: 39 additions & 0 deletions testdata/composition/expected.json
Expand Up @@ -110,6 +110,26 @@
}
}
}
},
"/testapi/get-foobarmap": {
"get": {
"description": "get FoorBarMap",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"operationId": "get-foo-bar-map",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.FooBarMap"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -151,6 +171,17 @@
}
}
},
"api.FooBarMap": {
"type": "object",
"properties": {
"field3": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/MapValue"
}
}
}
},
"api.FooBarPointer": {
"type": "object",
"properties": {
Expand All @@ -164,6 +195,14 @@
"type": "string"
}
}
},
"api.MapValue": {
"type": "object",
"properties": {
"field4": {
"type": "string"
}
}
}
}
}

0 comments on commit 68ab45d

Please sign in to comment.