Drf-yasg: Proper way to set baseUrl when using urlconf

Created on 13 Aug 2019  路  3Comments  路  Source: axnsan12/drf-yasg

I've been trying to expose my api via swagger schema view.

When I set the urlconf to the api urls:
get_schema_view(..., urlconf='management_server.api.urls)'
the basePath for those urls does not get set so the urls incorrectly show:
http://localhost:8800/client/ instead of the correct http://localhost:8800/api/client

before, I was able to set get_schema_view(..., url='/api') and the baseUrl correctly included /api but it looks like now the url requires a fully qualified url and the path portion is dropped anyway.

The docs seem to imply that the proper way is to use the FORCE_SCRIPT_NAME django setting to set the base url but this then affects the entirety of the django site by injecting /api in every path which feels wrong for setting the api path in this specific view.

api/views.py

schema_view = get_schema_view(
  openapi.Info(
    title="Management Server API",
    default_version='v1',
    contact=openapi.Contact(email="[email protected]"),
  ),
  public=False,
  permission_classes=(permissions.IsAuthenticated,),
  urlconf="management_server.api.urls",
)

api/urls.py

urlpatterns = [
    path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    ...,
]

Most helpful comment

A hacky workaround for now:

from rest_framework import permissions
from drf_yasg.generators import OpenAPISchemaGenerator
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
import os

class SchemaGenerator(OpenAPISchemaGenerator):
  def get_schema(self, request=None, public=False):
    schema = super(SchemaGenerator, self).get_schema(request, public)
    schema.basePath = os.path.join(schema.basePath, 'api/')
    return schema

schema_view = get_schema_view(
  openapi.Info(
    title="Management Server API",
    default_version='v1',
    contact=openapi.Contact(email="[email protected]"),
  ),
  public=False,
  permission_classes=(permissions.IsAuthenticated,),
  urlconf="management_server.api.urls",
  generator_class=SchemaGenerator,
)

All 3 comments

A hacky workaround for now:

from rest_framework import permissions
from drf_yasg.generators import OpenAPISchemaGenerator
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
import os

class SchemaGenerator(OpenAPISchemaGenerator):
  def get_schema(self, request=None, public=False):
    schema = super(SchemaGenerator, self).get_schema(request, public)
    schema.basePath = os.path.join(schema.basePath, 'api/')
    return schema

schema_view = get_schema_view(
  openapi.Info(
    title="Management Server API",
    default_version='v1',
    contact=openapi.Contact(email="[email protected]"),
  ),
  public=False,
  permission_classes=(permissions.IsAuthenticated,),
  urlconf="management_server.api.urls",
  generator_class=SchemaGenerator,
)

@Safrone Thanks for the hack!

Hack works great.
Would be even greater to have a clean way however :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beaugunderson picture beaugunderson  路  5Comments

littlehome-eugene picture littlehome-eugene  路  4Comments

therefromhere picture therefromhere  路  3Comments

phihag picture phihag  路  3Comments

tiltec picture tiltec  路  4Comments