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.4k stars 438 forks source link

Swagger does not show ModelViewSet. #420

Open christianmtr opened 5 years ago

christianmtr commented 5 years ago

I have a project with two apps. Each app have their viewsets and api_view's. There are the files:

# project's urls.py
schema_view = get_schema_view(
    openapi.Info(),
    ...
)
urlpatterns = [
                  path('admin/', admin.site.urls),
                  path('api/', include([
                      path('', include('app1.urls')),
                      path('', include('app2.urls')),
                  ])),
                  re_path(r'^api-docs/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
              ]
# app1

# urls.py
urlpatterns = [
    path('view_one/', api_view_1),
    path('view_two/', api_view_2),
]

# views.py
@api_view(['POST'])
def api_view_1(request):
    return Response()

@api_view(['POST'])
def api_view_2(request):
    return Response()
# app2

# urls.py
router = routers.SimpleRouter()
router.register('model_one', viewsets.ModelOneViewSet, basename='model_one')

urlpatterns = [
    path('content/', include(router.urls)),
]
# views.py
class ModelOneViewSet(viewsets.ModelViewSet):
    queryset = ModelOne.objects.all()
    serializer_class = ModelOneSerializer
    permission_classes = (AllowAny,)

When I open api-docs/ page only show api1 routes (api/view_one/ and api/view_two/), even using re_path instead of path or including app2's routes in app1.urls.

The endpoints of app1 and app2 works fine when I make a request with curl.

What can I do? I'm missing anything?

marcelmu commented 4 years ago

I was actually wondering the same.

In my App's urls.py I'm registering my routes using the DefaultRouter

router = DefaultRouter()
router.register(r'events', views.EventViewSet)
...
urlpatterns = router.urls

In the project's urls.py I am adding those using the include() function:

urlpatterns = [
   path(r'api/v1/', include('youcan.urls')),
   ...
]