Skip to content

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

Merged
merged 1 commit into from
Jul 24, 2019

Conversation

dalvtor
Copy link
Contributor

@dalvtor dalvtor commented Jul 17, 2019

Description

Fixes #6815

The array type in OpenAPI should come with corresponding items. Currently, if a serializers.ListField is used, the documentation will display the message 'Could not render this component'. In this commit I just add the items with the proper type of the child so it renders correctly.

@rpkilby
Copy link
Member

rpkilby commented Jul 17, 2019

I'm inclined to say we should have a test for this. @carltongibson?

@carltongibson carltongibson self-requested a review July 18, 2019 07:23
@carltongibson
Copy link
Collaborator

Yes. Just a small test case directly passing a list field into _map_field would be fine.

@dalvtor
Copy link
Contributor Author

dalvtor commented Jul 18, 2019

I added a few tests and fixed a typo

@@ -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')
},
Copy link
Member

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?

Copy link
Contributor Author

@dalvtor dalvtor Jul 18, 2019

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

Copy link
Member

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": {})

Copy link
Contributor Author

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):

carltongibson pushed a commit to carltongibson/django-rest-framework that referenced this pull request Jul 20, 2019
@carltongibson carltongibson force-pushed the map-listfield-openapi branch from b4bbbb5 to 35db743 Compare July 20, 2019 20:32
Copy link
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dalvtor. Thanks for this.

I pulled the typo fix into #6827 and then pushed a few edits here, testing the mapping more directly, and adjusting for Tom's review.

LGTM! 👍

@carltongibson
Copy link
Collaborator

Ah, well of course I broke the lint... 🙂. I’ll run isort on it tomorrow 🙃

@carltongibson carltongibson force-pushed the map-listfield-openapi branch from 35db743 to eb9d311 Compare July 21, 2019 06:51
@carltongibson
Copy link
Collaborator

OK, fixed.

@dalvtor
Copy link
Contributor Author

dalvtor commented Jul 21, 2019

If we leave it out, Swagger UI will complain with the following message:

😱 Could not render this component, see the console.

I've added 'items': {} and amended the test. It works properly after that.

@carltongibson carltongibson force-pushed the map-listfield-openapi branch from 528bc2d to 6426d38 Compare July 21, 2019 18:23
@carltongibson
Copy link
Collaborator

OK, I see Tom had (or use "items": {}). Thanks.

@carltongibson
Copy link
Collaborator

OK, I'm going to merge this. Thank you @dalvtor!

@carltongibson carltongibson merged commit 2138f55 into encode:master Jul 24, 2019
carltongibson pushed a commit that referenced this pull request Jul 24, 2019
pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull request Nov 17, 2020
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI AutoSchema does not handle ListField correctly
4 participants