Skip to content

Swagger-UI generates wrong URL's when BASE_PATH is used #21354

@dxks

Description

@dxks

NetBox Edition

NetBox Community

NetBox Version

v4.5.1

Python Version

3.12

Steps to Reproduce

When running multiple NetBox-Versions (for testing) under the same host the BASE_PATH (actually the version) is used to distinguish.

In the environment the BASE_PATH is set to "/v4.4.7" and within the Docker-Container it is inserted into the config via the extra.py as the netbox-docker container no longer supports BASE_PATH

The Swagger-UI uses a schema with absolute paths:

{
    "openapi": "3.0.3",
    "info": {
        "title": "NetBox REST API",
        "version": "4.4.7-Docker-3.4.2 (4.4)",
        "license": {
            "name": "Apache v2 License"
        }
    },
    "paths": {
        "/v4.4.7/api/circuits/circuit-group-assignments/": {
            "get": {
                "operationId": "circuits_circuit_group_assignments_list",
...

The Swagger UI will be configured in settings.py with the relative BASE_PATH as the / is stripped of earlier:

SPECTACULAR_SETTINGS = {
    'TITLE': 'NetBox REST API',
    'LICENSE': {'name': 'Apache v2 License'},
    'VERSION': RELEASE.full_version,
    'COMPONENT_SPLIT_REQUEST': True,
    'REDOC_DIST': 'SIDECAR',
    'SERVERS': [{
        'url': BASE_PATH, <<<< this is now "v4.4.7/"
        'description': 'NetBox',
    }],
    'SWAGGER_UI_DIST': 'SIDECAR',
    'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
    'POSTPROCESSING_HOOKS': [],
}

Expected Behavior

The Swagger UI should render the URL correctly

curl -X 'GET' \
  'https://netbox.server.com/v4.4.7/api/circuits/circuit-group-assignments/' \
  -H 'accept: application/json'

This requires either:

  • remove the BASE_PATH in the Schema and use f"/{BASE_PATH}" as Server-Value in the Swagger-Config (like it's done at other locations, or
  • leave the Server Config empty

Observed Behavior

As the BASE_PATH is at this time v4.4.7/ the generated URL in the Try Out will end up in

current URL of Swagger + relative BASE_PATH + absolute API-Endpoint

curl -X 'GET' \
  'https://netbox.server.com/v4.4.7/api/schema/v4.4.7/v4.4.7/api/circuits/circuit-group-assignments/' \
  -H 'accept: application/json'

Metadata

Metadata

Assignees

Labels

netboxseverity: lowDoes not significantly disrupt application functionality, or a workaround is availablestatus: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions