Skip to content
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

OpenApiYamlRenderer cannot dump OrderedDict #1158

Closed
timothymctim opened this issue Jan 26, 2024 · 2 comments
Closed

OpenApiYamlRenderer cannot dump OrderedDict #1158

timothymctim opened this issue Jan 26, 2024 · 2 comments

Comments

@timothymctim
Copy link

The renderers.OpenApiYamlRenderer renderer cannot dump collections.OrderedDict objects. This is the case because the Dumper class inside the render function is inherited from yaml.SafeDumper, which doesn’t support collections.OrderedDict (yaml.Representer does).

To Reproduce
This bug is triggered in my setup when sending an OPTIONS request to the URL for the SpectacularAPIView. I am not entirely sure if this is caused in every setup or if this is specific to my setup.

Making an OPTIONS request, causes the OpenApiYamlRenderer.render function to receive a collections.OrderedDict type, causing the exception yaml.representer.RepresenterError.

To Fix
One of the approaches to address this issue is to add the represent_ordered_dict dumper from yaml.Representer in OpenApiYamlRenderer.render function, e.g.,

Dumper.add_representer(collections.OrderedDict, yaml.Representer.represent_ordered_dict)

Unrelated note: you might also want to change the parameter name media_type to accepted_media_type, since it is called that way in https://github.com/encode/django-rest-framework/blob/master/rest_framework/renderers.py. This was observed by Pyright, see also https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportIncompatibleMethodOverride.

@tfranzel
Copy link
Owner

Hey,

so since OrderedDict is a native type we should cover it imho, even though it not happening during normal usage.

I didn't know that the OPTIONS request actually uses the renderer (here yaml) of the view. On first sight, this feels like a DRF design flaw.

Unrelated note: you might also want to change the parameter name media_type to accepted_media_type

👍

tfranzel added a commit that referenced this issue Jan 28, 2024
@timothymctim
Copy link
Author

Thanks for the quick fix! Can confirm this fixes the issue I’ve had! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants