drf-spectacular is using the wrong AutoSchema to generate Swagger
Asked Answered
P

6

15

Previously I was using drf-yasg but want to update to use OpenAPI 3. I am trying to switch over to drf-spectacular. Following the instruction, I ran pip install drf-spectacular, I've removed all references to the drf-yasg package, and updated Settings.py as follows:

INSTALLED_APPS = [ 
    ...
    "drf_spectacular",
]


REST_FRAMEWORK = {
    "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}

When I use the CLI to generate the schema, I get the bellow AssertionError. If anyone has run into this problem before and has any insight, it would be much appreciated!

I'm using Python 3.7, Django 3.0, Django Rest Framework 3.11, and DRF Spectacular 0.10.0.

Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    main()
  File "manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/drf_spectacular/management/commands/spectacular.py", line 50, in handle
    schema = generator.get_schema(request=None, public=True)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/drf_spectacular/generators.py", line 187, in get_schema
    paths=self.parse(request, public),
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/drf_spectacular/generators.py", line 160, in parse
    'Incompatible AutoSchema used on View. Is DRF\'s DEFAULT_SCHEMA_CLASS '
AssertionError: Incompatible AutoSchema used on View. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?
Personality answered 26/10, 2020 at 22:30 Comment(2)
Hello. Did you ever solve this? Thanks.Cygnet
I'm having this problem tooTripitaka
M
2

Please update the Django Rest Framework 3.11 to 3.12 it will work.

Micropathology answered 6/11, 2020 at 10:56 Comment(2)
same issue on rest_framework.VERSION '3.12.2'Derrickderriey
If you see this error for >=3.12.0 it likely has a different cause. Please see the sidenote in my answer.Mealy
J
1

I have the same issue when I had REST_FRAMEWORK two times written for different settings in my settings.py. I moved everything in one variable and error is gone

Jody answered 4/8, 2022 at 10:36 Comment(2)
For me it was even enough to have an import from rest_framework.pagination import … to trip it up. Glad I found this 👍Inainability
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:Corell
M
1

If the AssertionError is about DRF's ObtainAuthToken then it is most likely an old bug in DRF. This issue was fixed in DRF>=3.12. Prior to that, DRF used a wrong class where it was not supposed to.

drf-yasg seems to not suffer from this upstream bug due to a different injection technique used. drf-spectacular has a mitigation for the bug starting with 0.24.0.

Related GH issue with workaround for older drf-spectacular versions: https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-1231464792

Sidenote: If this does not fix your problem and/or it is the same Assertion for some other view, you likely have a misconfigured settings.py. Make sure DEFAULT_SCHEMA_CLASS is properly set as stated in the README. Also make sure you are not shooting yourself in the foot by not setting this also in your production settings file. If the problem still persists, please open an issue on Github and get help there.

Mealy answered 30/8, 2022 at 10:38 Comment(0)
I
0

Check if you're importing from rest_framework!

Having a

from rest_framework.pagination import PageNumberPagination

was all it took to provoke the error. As soon as I removed the import, things started working again.

This even applies when importing other modules which in turn import from rest_framework. I ended up using the "string imports" like so:

"DEFAULT_PAGINATION_CLASS": "api.pagination.DefaultPagination",
Inainability answered 28/12, 2022 at 11:37 Comment(0)
D
0

I had the same issue. Just make sure you don't have REST_FRAMEWORK defined more than once in your settings.py file.

Denys answered 26/5, 2023 at 6:24 Comment(0)
C
0

In my case, I had to disable extra_classes in django_stubs_ext. From:

import django_stubs_ext
from rest_framework import viewsets

django_stubs_ext.monkeypatch(extra_classes=(viewsets.ModelViewSet,))

To:

import django_stubs_ext

django_stubs_ext.monkeypatch()
Christianna answered 7/3 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.