Reputation: 88529
I am using drf-yasg to document my APIs and I have the following setup,
from django.urls import path
from drf_yasg.views import get_schema_view
public_schema_view = get_schema_view(..., urlconf='public_apis.urls')
private_schema_view = get_schema_view(..., urlconf='private_apis.urls')
urlpatterns = [
path('public/', public_schema_view.with_ui('swagger', cache_timeout=0), name='schema-public'),
path('private/', private_schema_view.with_ui('swagger', cache_timeout=0), name='schema-private'),
]
Note: The public_schema_view
and private_schema_view
are renedered two different set of URLs from the modules public_apis.urls
and private_apis.urls
respectively.
Here I want to have two basePath
s,
/api/v1/public/
for public_schema_view
and/api/v1/private/
for private_schema_view
So, the question is, how can I set multiple custom basePath
in drf-yasg ?
Upvotes: 1
Views: 2471
Reputation: 88529
You can set the basePath
by override the get_schema(...)
method of schema generator class and it can be used in the get_schema_view(...)
function by using generator_class
argument.
The schema generator class can be created by inheriting drf_yasg.generators.OpenAPISchemaGenerator
class as,
from django.urls import path
from drf_yasg.views import get_schema_view
from drf_yasg.generators import OpenAPISchemaGenerator
class PublicAPISchemeGenerator(OpenAPISchemaGenerator):
def get_schema(self, request=None, public=False):
schema = super().get_schema(request, public)
schema.base_path = '/api/v1/public/'
return schema
class PrivateAPISchemeGenerator(OpenAPISchemaGenerator):
def get_schema(self, request=None, public=False):
schema = super().get_schema(request, public)
schema.base_path = '/api/v1/private/'
return schema
public_schema_view = get_schema_view(...,
urlconf='public_apis.urls',
generator_class=PublicAPISchemeGenerator)
private_schema_view = get_schema_view(...,
urlconf='private_apis.urls',
generator_class=PrivateAPISchemeGenerator)
urlpatterns = [
path('public/', public_schema_view.with_ui('swagger', cache_timeout=0), name='schema-public'),
path('private/', private_schema_view.with_ui('swagger', cache_timeout=0), name='schema-private'),
]
Upvotes: 3