Fixes #21354: Fix Swagger-UI generating wrong URLs when BASE_PATH is set #389

Closed
opened 2026-04-05 16:29:01 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @adionit7 on 2/10/2026

Fixes

Closes #21354

Summary

When BASE_PATH is configured (e.g., v4.4.7), Swagger-UI generates incorrect request URLs with the base path duplicated multiple times:

https://netbox.server.com/v4.4.7/api/schema/v4.4.7/v4.4.7/api/circuits/...

Root cause: The SPECTACULAR_SETTINGS['SERVERS'] url was set to BASE_PATH (e.g., v4.4.7/). However, drf-spectacular already includes the BASE_PATH prefix in the schema paths since all URL patterns are wrapped under path(settings.BASE_PATH, include(_patterns)). Swagger-UI resolves the relative server URL against the document URL and then appends the path, causing the prefix to appear multiple times.

Fix: Replace 'url': BASE_PATH with 'url': '' in the SERVERS configuration. An empty string causes Swagger-UI to resolve paths relative to the current document, and since the schema paths are already absolute (e.g., /v4.4.7/api/circuits/...), they resolve correctly to the server root.

This is a no-op for the default case (no BASE_PATH), since BASE_PATH already evaluates to '' when unset.

*Originally created by @adionit7 on 2/10/2026* ## Fixes Closes #21354 ## Summary When `BASE_PATH` is configured (e.g., `v4.4.7`), Swagger-UI generates incorrect request URLs with the base path duplicated multiple times: ``` https://netbox.server.com/v4.4.7/api/schema/v4.4.7/v4.4.7/api/circuits/... ``` **Root cause:** The `SPECTACULAR_SETTINGS['SERVERS']` url was set to `BASE_PATH` (e.g., `v4.4.7/`). However, drf-spectacular already includes the `BASE_PATH` prefix in the schema paths since all URL patterns are wrapped under `path(settings.BASE_PATH, include(_patterns))`. Swagger-UI resolves the relative server URL against the document URL and then appends the path, causing the prefix to appear multiple times. **Fix:** Replace `'url': BASE_PATH` with `'url': ''` in the `SERVERS` configuration. An empty string causes Swagger-UI to resolve paths relative to the current document, and since the schema paths are already absolute (e.g., `/v4.4.7/api/circuits/...`), they resolve correctly to the server root. This is a no-op for the default case (no `BASE_PATH`), since `BASE_PATH` already evaluates to `''` when unset.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#389