-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
add support for map nested in struct #521
Conversation
Codecov Report
@@ Coverage Diff @@
## master #521 +/- ##
=========================================
+ Coverage 87.24% 87.3% +0.06%
=========================================
Files 7 7
Lines 1560 1599 +39
=========================================
+ Hits 1361 1396 +35
- Misses 117 119 +2
- Partials 82 84 +2
Continue to review full report at Codecov.
|
3002476
to
93f48ba
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you help add tests for it?
I added a FooBarMap struct to the api in composition test. With view-covered I see that the tests the covers the added code. Except for when the map is required or the parsing of the value fails. If that doesn't suffice, can you indicate which part should further tested and where to add it? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see dictionaries spec, The actual code inserts a definition to the object "api.MapValue" that is not referenced anywhere.
} | ||
} | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definition
type FooBarMap struct {
Field3 map[string]MapValue
}
type MapValue struct {
Field4 string
}
should produce
api.FooBarMap:
type: object
properties:
field3:
type: object
additionalProperties:
$ref: '#/definitions/api.MapValue'
api.MapValue:
type: object
properties:
field4:
type: string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be indeed a better approach. Although I see if there is a map in operations it doesn't use the reference, but also inlines the value.
So to do this. the scheme of the additionalProperties needs to be something like:
mapValueScheme := &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + pkgName + "." + ???),
},
},
}
But I have some questions about how to get the ???.
With itemSchema, err := parser.parseTypeExpr(pkgName, "", astTypeMap.Value)
the MapValue is parsed and added to the definitions. The name api.MapValue is refTypeName := fullTypeName(pkgName, expr.Name)
in the case that astTypeMap.Value is of type *ast.Ident. Although I don't see a clear way to get the name api.MapValue from either itemSchema
or astTypeMap.Value
. Is there are already a function that returns "api.MapValue"?
I case no, what would be the best solution?
- Write a function that returns api.MapValue given astTypeMap?
- Or let
parser.parseTypeExpr
also return the definition key (api.MapValue)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will register your api.MapValue to definitions
itemSchema, err := parser.parseTypeExpr(pkgName, "", astTypeMap.Value)
if err != nil {
return properties, nil, err
}
Neighter of 2 solutions.
You need to use the astTypeMap.Value to add it to object..
fullTypeName, err := getFieldType(astTypeMap.Value)
if err != nil {
return ....
}
mapValueScheme := &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + fullTypeName),
},
},
}
I'm quite busy these days and I don't have much time to spend here. I hope i was clear enough with the last response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You made it clear. Almost did it yourself ><
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Mappings nested in structs are not generated with additional properties. If a field is of a MapType, add the field with additional properties. If needed add the key and value type to the scheme.
Relation issue
#517