-
Notifications
You must be signed in to change notification settings - Fork 269
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
When DEFAULT_VERSIONING_CLASS is set to "AcceptHeaderVersioning" the SpectacularSwaggerView is inaccessible #1036
Comments
Good point @PureTryOut, the In the meantime you can do path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema', versioning_class=None), name='swagger-ui'), , which should have the same effect as the upcoming fix. |
Hmm that doesn't seem to work. Well Swagger loads again, but then complains that |
That was to be expected, since you do not have a default version and that makes perfect sense. You hard require a version for your API while not setting a default. The schema should not work as we cannot know what you want. There are two options. hardcode the version path('api/schema/', SpectacularAPIView.as_view(api_version="v1"), name='schema'), OR use the swagger topbar: SPECTACULAR_SETTINGS = {
"SERVE_INCLUDE_SCHEMA": False,
"SWAGGER_UI_SETTINGS": '''{
deepLinking: true,
urls: [{url: "/api/schema/?version=v1", name: "v1"}, {url: "/api/schema/?version=v2", name: "v2"}],
presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
layout: "StandaloneLayout",
persistAuthorization: true,
displayOperationId: true,
filter: true
}''',
} Note that you still need the |
I'd like to use that topbar, but those urls examples don't work with |
they do (indirectly). that is a feature of the schema endpoint. that version param should be handed over to the appropriate places. It certainly is not just forwarded as a param to the API endpoints.
Hmm that should not be hit anymore. Strange. Have you tried the hardcoded version also? Internally they are almost the same and that route might be easier to debug. |
I could hardcode the version for the schema, e.g. |
I'm not suggesting you should should choose that as a permanent solution. I'm just asking you to test this variant to further investigate the reasons why this is not working for you. this is just a more straight forward override of the version with one step less that is internally identical: drf-spectacular/drf_spectacular/views.py Line 88 in 33eb5c6
|
nevermind my last comment, it will not work either way. I think i understood it. When using Your settings require a 3 options:
This is not a bug but a (non-obviously) broken configuration on your part. There is basically nothing we can do to mitigate this in our end. |
ahh wait you can even do path("api/schema/", SpectacularAPIView.as_view(versioning_class=None),
path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema', versioning_class=None), name='swagger-ui'), and on swagger config
and it should work. the version gets picked up from the url and used even if you do not have a versioning class set on our views. This circumvents rejected requests due to a missing header while still utilizing the version from the query param. |
That works great! Thank you for the help, this makes things a lot easier 😄 |
Even though this change makes sense in principle, it may be a regression and having the HTML views have versioning classes does make sense in certain situations. Revert the change as this is not a clear improvement on closer inspection. This reverts commit 388da8d.
Describe the bug
I'm trying to version the endpoints:
With these settings the SpectacularSwaggerView becomes inaccessible, as it is internally a DRF view which now requires the
accept
header to containversion=v1
. The browser of course doesn't set that so when navigating to that page I now get406 Not Acceptable
.Versioning makes sense for every endpoint except for the Swagger view itself. If I set the default version the view works fine.
To Reproduce
Set
DEFAULT_VERSIONING_CLASS
torest_framework.versioning.AcceptHeaderVersioning
and make sure no default version is set.Expected behavior
The Swagger view to be available at all times, no matter if the API version is set in the request headers or not.
The text was updated successfully, but these errors were encountered: