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

Override SWAGGER_UI_SETTINGS via custom_settings #1341

Open
stefanofusai opened this issue Dec 3, 2024 · 0 comments
Open

Override SWAGGER_UI_SETTINGS via custom_settings #1341

stefanofusai opened this issue Dec 3, 2024 · 0 comments

Comments

@stefanofusai
Copy link

I'm looking for a way to override SWAGGER_UI_SETTINGS for a given SpectacularSwaggerView as I want to serve different schemas for admins and customers.

My urls.py look like this:

urlpatterns = [
    ...
    path("docs/", SpectacularSwaggerView.as_view(url_name="v4-schema"), name="docs"),
    path(
        "docs/customers/",
        SpectacularSwaggerView.as_view(url_name="v4-customers-schema"),
        name="customers-docs",
    ),
    path(
        "schema/v3/",
        SpectacularAPIView.as_view(
            api_version="v3", custom_settings={"VERSION": "3.0.0"}
        ),
        name="v3-schema",
    ),
    path(
        "schema/v4/",
        SpectacularAPIView.as_view(
            api_version="v4", custom_settings={"VERSION": "4.0.0"}
        ),
        name="v4-schema",
    ),
    path(
        "schema/customers/v3/",
        SpectacularAPIView.as_view(
            api_version="v3",
            custom_settings={
                "APPEND_COMPONENTS": {
                    "securitySchemes": {
                        "ApiKeyAuth": {
                            "type": "apiKey",
                            "in": "header",
                            "name": "X-API-Key",
                        }
                    }
                },
                "AUTHENTICATION_WHITELIST": ["src.customers.permissions.HasAPIKey"],
                "SCHEMA_PATH_PREFIX": r"^(/customers/v3/)$",
                "VERSION": "3.0.0",
            },
            serve_public=False, # Customer will need to Authorize in the UI via API Key
        ),
        name="v3-customers-schema",
    ),
    path(
        "schema/customers/v4/",
        SpectacularAPIView.as_view(
            api_version="v4",
            custom_settings={
                "APPEND_COMPONENTS": {
                    "securitySchemes": {
                        "ApiKeyAuth": {
                            "type": "apiKey",
                            "in": "header",
                            "name": "X-API-Key",
                        }
                    }
                },
                "AUTHENTICATION_WHITELIST": ["src.customers.permissions.HasAPIKey"],
                "SCHEMA_PATH_PREFIX": r"^(/customers/v3/)$",
                "VERSION": "4.0.0",
            },
            serve_public=False, # Customer will need to Authorize in the UI via API Key
        ),
        name="v4-customers-schema",
    )
]

And my SPECTACULAR_SETTINGS look like this:

SPECTACULAR_SETTINGS = {
    ...,
    "SERVE_INCLUDE_SCHEMA": False,
    "SWAGGER_UI_SETTINGS": """{
        defaultModelsExpandDepth: -1,
        deepLinking: true,
        displayRequestDuration: true,
        layout: "StandaloneLayout",
        persistAuthorization: true,
        presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
        requestSnippetsEnabled: true,
        tagsSorter: "alpha",
        urls: [{url: "/schema/v4/", name: "v4"}, {url: "/schema/v3/", name: "v3"}],
        validatorUrl: null
    }""",
	...
}

The issue that I'm encountering is that due to my API having versioning enabled via SWAGGER_UI_SETTINGS.urls I have no way of overriding the schema urls for the customers-docs view as it will always point to urls: [{url: "/schema/v4/", name: "v4"}, {url: "/schema/v3/", name: "v3"}].
My goal is to somehow set urls: [{url: "/schema/customers/v4/", name: "v4"}, {url: "/schema/customers/v3/", name: "v3"}] for the customers-docs view, but that doesn't seem to be possible via custom_settings.

image

Am I missing something? Is there a better way to achieve what I'm trying to achieve?
Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant