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

a problem with creating a schema with drf 3.10.3 #796

Closed
fannyhub opened this issue Aug 26, 2022 · 13 comments
Closed

a problem with creating a schema with drf 3.10.3 #796

fannyhub opened this issue Aug 26, 2022 · 13 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@fannyhub
Copy link

fannyhub commented Aug 26, 2022

Describe the bug
I have used drf version 3.10.3 as suggested and my previous problem has been resolved.
Alas, the schema still does not get built, failing with:
AssertionError: Incompatible AutoSchema used on View <class 'view-name'>. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?

This happens although I have set DEFAULT_SCHEMA_CLASS in REST_FRAMEWORK settings to drf_spectacular.openapi.AutoSchema.

To Reproduce
create a project with drf 3.10.3 (Django 2.2, Python 3.8/3.9)
Execute ./manage.py spectacular --file schema.yml

Expected behavior
I would expect the schema to pass smoothly

The same applies to 3.11.2 drf version

@tfranzel
Copy link
Owner

Hi,

every time this comes up, it is a different settings.py file used then what was assumed or a settings.py file that overrides what you originally had. This is usually due to environment variable DJANGO_SETTINGS_MODULE.

REST_FRAMEWORK = {
    # YOUR SETTINGS
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

will 100% work. Just make sure you have the right settings file loaded.

@fannyhub
Copy link
Author

thank you for such a quick reply. The variable seems not to be overriden:
image

@tfranzel
Copy link
Owner

That is the usual fallacy. running ./manage.py runserver and ./manage.py shell might load different settings depending on your setup. Sometimes simply by the prescence of a different DJANGO_SETTINGS_MODULE environment variable. It is a common issue. Try to use the debugger on your actual webserver process if possible. I'm quite confident it will be different in there.

@fannyhub
Copy link
Author

Alas, it is not possible that we touch this variable. btw, we do not use runserver

@tfranzel
Copy link
Owner

tfranzel commented Aug 29, 2022

Alas, it is not possible that we touch this variable.

not sure what that means.

btw, we do not use runserver

with runserver I meant your server process. Otherwise just try to print that variable to console in the process, where this happens.

Either you have some custom schema class on views OR your DEFAULT_SCHEMA_CLASS is not properly set in the environment. I cannot see any other explanation. Again, this has come up at least a dozen times. Every time it was a misconfigured settings.py. This works for hundreds of people without issue so it has to be something in your setup/environment I suppose.

@fannyhub
Copy link
Author

Thank you very much for another reply. By not touching I meant that we didn't change the DEFAULT_SCHEMA_CLASS - it was introduced very recently, to see if transitioning to drf-spectacular was possible in our project.
The thing is that the problem disappears with the upgrade of rest framework to version 3.12.4 or 3.13 and schema gets generated. So I don't believe it depends upon our code. Please consider that some other users have encountered it, too and solved it by drf's upgrade: https://stackoverflow.com/questions/64545861/drf-spectacular-is-using-the-wrong-autoschema-to-generate-swagger . Furthermore, as the AssertionError is raised by the line (in drf-spectacular) assert isinstance(view.schema, AutoSchema) I found out that the view.schema is drf's ManualSchema. in version 3.10.3, ManualSchema usage in ObtainTokenView looks like that:
image

meanwhile in version 3.13.1 version of drf, this code looks as follows:
image

It seems that:

  1. in the first case - i.e. drf 3.10.3. - the condition (if coreapi is not None and coreschema is not None) is True
  2. while in the second case - i.e. drf 3.13.1 - (if coreapi.is_enabled() - the condition is False. In is_enabled() it is checked if api_settings.DEFAULT_SCHEMA_CLASS , is subclass of rest_framework.schemas.coreapi.AutoSchema. Debugging shows that api_settings.DEFAULT_SCHEMA_CLASS is drf_spectacular.openapi.AutoSchema.

@tfranzel
Copy link
Owner

tfranzel commented Aug 30, 2022

https://stackoverflow.com/questions/64545861/drf-spectacular-is-using-the-wrong-autoschema-to-generate-swagger

Thanks @fannyhub for digging in. Sorry that I nudged you in the wrong direction repeatedly. 99% of raised&resolved issues were indeed wrong configs. I was not aware of that SO question and apparently nobody raised it here.

Your analysis is absolutely correct. It is indeed a bug in DRF < 3.12. I added an extension that fixes that bug for prior versions. It will be in the next release (0.24.0). Until then, you can easily fix it yourself by emulating what the extension will do:

        from drf_spectacular.openapi import AutoSchema
        from rest_framework.authtoken.views import ObtainAuthToken

        class FixedObtainAuthToken(ObtainAuthToken):
            schema = AutoSchema()

and using that in your urlpatterns instead of ObtainAuthToken/obtain_auth_token. Or you could simply copy&paste that extension for the time being.

tfranzel added a commit that referenced this issue Aug 30, 2022
mitigate DRF bug in ObtainAuthToken < 3.12.0 #796
@tfranzel tfranzel added bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Aug 30, 2022
@fannyhub
Copy link
Author

Thank you, no more problems with creating schema :)

@kelvinmacharia254
Copy link

AssertionError: Incompatible AutoSchema used on View <class 'Vendors.views.PurchaseOrderListCreateView'>. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?

Still have this issue in Django reset 3.14.0 and drf_spectacular 0.27.0 .

I have have configured everything right as discussed here and as per documentation but no solution:

fork repo @:

https://github.com/Taruna21/Vendor_Management_System

@tfranzel
Copy link
Owner

this is an uncommon error and the message is exactly right. Some view, even from a library may set a custom schema class on a view and that provokes the error. If you are not doing it, some library does probably. This happened once before but I don't recall which lib. Maybe updating your libs will already fix the issue.

@kelvinmacharia254
Copy link

kelvinmacharia254 commented Jan 15, 2024 via email

@tfranzel
Copy link
Owner

please find a minimal reproduction (the offending view) including stacktrace and create a new issue. I have very limited time to look at this.

@kelvinmacharia254
Copy link

kelvinmacharia254 commented Jan 15, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

3 participants