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'),
...,
]
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 :)
Most helpful comment
A hacky workaround for now: