drf-spectacular: a problem with creating a schema with drf 3.10.3

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (8 by maintainers)

Commits related to this issue

Most upvoted comments

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.

Resolved, I had this from drf_spectacular.openapi import AutoSchema in my import in settings.py. Removed it and everything works fine

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.

I am glad my tip helped

On Sat, 1 Mar 2025, 12:26 pm Joshua Nwokoye (Atoms), < @.***> wrote:

Resolved, I had this from drf_spectacular.openapi import AutoSchema in my import in settings.py. Removed it and everything works fine

— Reply to this email directly, view it on GitHub https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-2692091672, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6A3F6GZFAJ735THXSLJSGL2SF4LFAVCNFSM6AAAAABW6NTTOCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOJSGA4TCNRXGI . You are receiving this because you commented.Message ID: @.***> [image: JAtoms]JAtoms left a comment (tfranzel/drf-spectacular#796) https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-2692091672

Resolved, I had this from drf_spectacular.openapi import AutoSchema in my import in settings.py. Removed it and everything works fine

— Reply to this email directly, view it on GitHub https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-2692091672, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6A3F6GZFAJ735THXSLJSGL2SF4LFAVCNFSM6AAAAABW6NTTOCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOJSGA4TCNRXGI . You are receiving this because you commented.Message ID: @.***>

Ah, I just set up a test project and it’s working. OK sorry about that, I’ll start going through my project and see where I messed up (starting with getting rid of drf-yasg).

On Thu, Feb 27, 2025 at 7:41 AM T. Franzel @.***> wrote:

@raparkhurst https://github.com/raparkhurst the fact that this fails for drf_spectacular.views.SpectacularAPIView tells me there is something fundamentally wrong in the setup. This is one of our views and has to work. If this fails, likely all views would fail.

I can see you have drf-yasg in there. It cannot work with both libraries active. You need to comment out everything related to drf-yasg including in the config. I have the feeling some yasg configuration is breaking everything here.

On a clean setup, it is almost impossible to break SpectacularAPIView. I’m pretty sure yasg injects its own schema class implementation somewhere and that breaks the whole thing.

— Reply to this email directly, view it on GitHub https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-2687999815, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ3MZBI6GC6PJWWSSRLWQD2R4IXRAVCNFSM6AAAAABW6NTTOCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOBXHE4TSOBRGU . You are receiving this because you were mentioned.Message ID: @.***> [image: tfranzel]tfranzel left a comment (tfranzel/drf-spectacular#796) https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-2687999815

@raparkhurst https://github.com/raparkhurst the fact that this fails for drf_spectacular.views.SpectacularAPIView tells me there is something fundamentally wrong in the setup. This is one of our views and has to work. If this fails, likely all views would fail.

I can see you have drf-yasg in there. It cannot work with both libraries active. You need to comment out everything related to drf-yasg including in the config. I have the feeling some yasg configuration is breaking everything here.

On a clean setup, it is almost impossible to break SpectacularAPIView. I’m pretty sure yasg injects its own schema class implementation somewhere and that breaks the whole thing.

— Reply to this email directly, view it on GitHub https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-2687999815, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ3MZBI6GC6PJWWSSRLWQD2R4IXRAVCNFSM6AAAAABW6NTTOCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOBXHE4TSOBRGU . You are receiving this because you were mentioned.Message ID: @.***>

Thank you, no more problems with creating schema 😃