axnsan12 / drf-yasg

Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
https://drf-yasg.readthedocs.io/en/stable/
Other
3.42k stars 438 forks source link

Proper way to set baseUrl when using urlconf #440

Open Safrone opened 5 years ago

Safrone commented 5 years ago

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="example@example.com"),
  ),
  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'),
    ...,
]
Safrone commented 5 years ago

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="itadmin@enertiv.com"),
  ),
  public=False,
  permission_classes=(permissions.IsAuthenticated,),
  urlconf="management_server.api.urls",
  generator_class=SchemaGenerator,
)
jsmedmar commented 4 years ago

@Safrone Thanks for the hack!

Schwankenson commented 3 years ago

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

Safrone commented 2 years ago

Noticed this PR though I'm not sure if it fixes this issue: https://github.com/axnsan12/drf-yasg/pull/682

AliBigdeli commented 1 year ago

well after all the issues i went through this is still an issue cause if you use one include inside urlpatterns it doesnt work, but if you have multiple it works 😒so again i stuck here!

its not working??!!?!? `urlpatterns = [

admin panel url

path("admin/", admin.site.urls),

# config app urls 
path("", include("farm_config.urls")),

}`

it is working! `urlpatterns = [

admin panel url

path("admin/", admin.site.urls),

# config app urls 
path("", include("farm_config.urls")),

# access control app urls
path("access-control/", include("access_control.urls")),

}`