Pass Swagger-UI url in drf-spectacular settings

2.5k views Asked by At

I have a project written in Django 2.x. I want to add automatic documentation to it using drf-spectacular. I'm using drf-spectacular=0.15.1 at the moment.

I'm running my project in Docker, and the build context for my api container is set as the root folder of the api project.

When I run the command to generate the schema, ./manage.py spectacular --file schema.yml, the script creates a schema.yml file in the root of my api project, but my Django settings.py are found one level below, in a folder called my_website. The structure of the project is like this:

main-project
\api
 \_my_website
  __init__.py
  apps.py
  settings.py
  urls.py

 schema.yml

\client
docker-compose.yml

So in settings.py I added specific settings to tell Swagger-UI where from to take the schema, as specified in the drf-spectacular documentation:

SPECTACULAR_SETTINGS = {
    'SWAGGER_UI_SETTINGS': {
        'url': 'schema.yml'
    },
}

Once I try to access the swagger-ui url in my project to check that the schema is loaded there, I get the error: **Fetch error** Not Found schema.yml. I tried passing the schema url in various ways, for example through relative url ../schema.yml but that didn't work either. Maybe somebody can give me an idea of what I'm doing wrong.

Thanks!

1

There are 1 answers

1
JPG On BEST ANSWER

From the swagger-configuration doc,

url: The URL pointing to API definition (normally swagger.json or swagger.yaml)

thus, the url value must be either a "relative URL" or "absolute URL".

For example

SPECTACULAR_SETTINGS = {
    'SWAGGER_UI_SETTINGS': {
        'url': 'https://example.com/path/to/schema/',  # absolute path
        'url': '/path/to/schema/',  # relative path
    },
}