-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow globally excluding certain content types #598
Comments
I ran into this same problem. I believe that this seems to be a problem many popular client generators:
There are a number of workarounds:
The last option can be achieved by overriding the from drf_spectacular.openapi import AutoSchema as _AutoSchema
from rest_framework.request import is_form_media_type
class AutoSchema(_AutoSchema):
def map_parsers(self) -> list[str]:
"""
Return a unique list of media types.
Overrides the default behaviour to remove form media types as done in ``drf_yasg.utils.get_consumes()``.
We could have changed DRF's ``DEFAULT_PARSER_CLASSES`` setting but we don't want to inadvertently break things.
The reason for implementing this hack is to work around broken client generation from ``swagger-codegen`` which
is used by editor.swagger.io and seems to be incapable of handling multiple media types in request bodies. As
this may be used by clients, we'll tweak this even though our specification is perfectly valid when unaltered.
It may be better to steer clients to use ``openapi-generator`` instead. This doesn't support multiple media
types in the generated client code, but it doesn't generate invalid output by using the first available media
type (which is ``application/json`` based on the default value of ``DEFAULT_PARSER_CLASSES``.
See the following links for additional information:
- https://github.com/swagger-api/swagger-codegen/issues/8191
- https://github.com/OpenAPITools/openapi-generator/issues/2668
- https://openapi-generator.tech/
"""
media_types = super().map_parsers()
non_form_media_types = [x for x in media_types if not is_form_media_type(x)]
return non_form_media_types if non_form_media_types else media_types This can then be configured: REST_FRAMEWORK = {
...
"DEFAULT_SCHEMA_CLASS": "<path>.<to>.AutoSchema",
...
} Hope that helps someone. |
@ngnpope Thank you for the discussion. I can confirm your final work around works well at addressing this issue. ❤️ |
Interesting! Generally i would say that it has helped me seeing the feature that are enabled on the API. At the end of the day these are the things your API actually supports. Regarding I usually immediately remove the these are the DRF defaults: 'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
], We already do explicitly ignore What do you guys think of this approach? |
A SPECTACULAR_SETTINGS = {
"PARSER_WHITELIST": ["rest_framework.parsers.JSONParser"],
} |
yes it would be exactly like that |
That sounds perfect to me, and likely very useful for many/most DRF users. Like you said, most people don't realize this is actually a problem because It would be a breaking change (for non- But for "purity" / "correctness", this should probably remain opt-in. |
Sounds like the settings would be useful. The only reason for this is that nearly all the client generators get it wrong and unfortunately users depend on them heavily. |
I think so too. Generally I look at the Swagger UI and adapt the API according to my expectations, which is mostly reducing the potential surface area of the API. I think this feature definitely has it's purpose, but i would generally recommend adapting the API and not the schema where possible. I added the two new settings. |
@tfranzel Confirmed that 0.21.0 fixes this issue. Thanks! Feel free to close this issue. |
This works well on a global level, but I think it's worth pointing out that my suggestion for overriding But as we've already discussed, it is the code generators that are broken, and these hacks are just one more of the ways that we can work around the fact that they cannot handle correct specifications. So no further changes needed. Just highlighting something that others may need to be aware of from my experience. |
By default, (I believe because DRF supports the Browsable Web API)
drf-spectacular
will generate a schema with bothform-data
andjson
content types:While the
form-data
type is used for the browseable web API, users might want to exclude it from the schema so that generated clients are simpler. Without an exclusion, multiple client code paths are generated which complicates the generated code and sometimes conflicts with one another:Adding a global setting to somehow disable certain content types could be useful to solve this issue:
This would allow the browsable API to continue to work (by not actually changing any views) but allow the exported DRF schema to be much simpler.
The text was updated successfully, but these errors were encountered: