-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Handles ListField mapping for OpenAPI #6819
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
Conversation
I'm inclined to say we should have a test for this. @carltongibson? |
Yes. Just a small test case directly passing a list field into _map_field would be fine. |
I added a few tests and fixed a typo |
rest_framework/schemas/openapi.py
Outdated
@@ -258,6 +258,9 @@ def _map_field(self, field): | |||
if isinstance(field, serializers.ListField): | |||
return { | |||
'type': 'array', | |||
'items': { | |||
"type": self._map_field(field.child).get('type') | |||
}, |
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.
Should we only be including items
if child
is not None
?
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.
if child
is None
the type will be string
.
items
key is required, otherwise the component will not be able to be rendered
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.
If child
is None
then any type is valid.
In JSON Schema you'd usally omit items
in that case (or use "items": {}
)
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.
Makes sense.
Since ListField
child is _UnvalidatedField
if omitted, should I just check the following?
if not isinstance(field.child, _UnvalidatedField):
Cherry-picked from encode#6819
b4bbbb5
to
35db743
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.
Ah, well of course I broke the lint... 🙂. I’ll run isort on it tomorrow 🙃 |
35db743
to
eb9d311
Compare
OK, fixed. |
If we leave it out, Swagger UI will complain with the following message:
I've added |
528bc2d
to
6426d38
Compare
OK, I see Tom had |
OK, I'm going to merge this. Thank you @dalvtor! |
Cherry-picked from #6819
Cherry-picked from encode#6819
Cherry-picked from encode#6819
Description
Fixes #6815
The
array
type in OpenAPI should come with correspondingitems
. Currently, if aserializers.ListField
is used, the documentation will display the message 'Could not render this component'. In this commit I just add theitems
with the proper type of the child so it renders correctly.