DRF-Spectacular with django-split-settings: AssertionError: Incompatible AutoSchema to generate Swagger

48 Views Asked by At

I am encountering an error when utilizing django-split-settings (version 1.3.0) and drf-spectacular (version 0.27.1) in my Django project. The error occurs when attempting to access the Swagger UI endpoint at api/schema/swagger-ui/.

Here is the error message:

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

To provide further context, here's a breakdown of my project structure:

my_project
|
|_apps
|
|_settings
|
|---components
|
|------drf.py
|
|---environments
|
|---__init__.py

The content of drf.py and init.py is as follows

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

init.py content::

from split_settings.tools import include
from my_project.settings.components.env import Environment

_base_settings = (
    # Load environment settings
    "components/env.py",
    # Here we should have the order because of dependencies
    "components/paths.py",
    "components/security.py",
    "components/apps.py",
    "components/middleware.py",
    # Load all other settings
    "components/*.py",
    # Select the right env:
    f"environments/{Environment}.py",
)

# Include settings:
include(*_base_settings)

It's important to note that this error doesn't occur when using a single, traditional settings file in my Django project. Here's the relevant portion of my urls.py file for reference (just same as the drf-spectacular package):

from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
urlpatterns = [
    # YOUR PATTERNS
    path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
    # Optional UI:
    path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]

What I Tried: I attempted to integrate django-split-settings (version 1.3.0) with drf-spectacular (version 0.27.1) to utilize split settings while still generating Swagger documentation for my Django project. I specifically configured DEFAULT_SCHEMA_CLASS in drf.py to point to drf_spectacular.openapi.AutoSchema.

Expected Outcome: I expected to be able to access the Swagger UI endpoint at api/schema/swagger-ui/ and view the generated API documentation for my project.

Actual Result: However, upon attempting to access the Swagger UI endpoint, I encountered the above AssertionError.

1

There are 1 best solutions below

1
Sinan Aslam On
restructuring your __init__.py like this to give

_base_settings = (
# Load environment settings
"components/env.py",
# Prioritize DRF-Specific settings
"components/drf.py", 
# Then load others
"components/paths.py",
"components/security.py",
"components/apps.py",
"components/middleware.py",
# ... other components
# Select the right env:
f"environments/{Environment}.py",

)

include(*_base_settings)