drf-spectacular: drf-spectacular does not detect any schema

Hello,

I am trying to generate an open Api documentation using your library, however I keep getting the No operations defined in spec! while using the ui. I believe it is an error due to my own implementation.

All my view can be find like this:

my app / views / view1.py view2.py

I mostly use the generics views, here is an example:

class SystemDetail(generics.RetrieveAPIView):
""" Get a system detail
User in group x can see all system.
Other groups have access to system with the corresponding dealer name.

For now it return Not Found id Not Authorized.
"""

lookup_field = 'uuid'

def get_queryset(self):
    user_groups = self.request.user.groups.all().values_list('name', flat=True)

    if 'x' not in user_groups:
        dealer = MANUFACTER_CHOICE
        dealer_name: str = [dealer_name[1] for dealer_name in dealer if dealer_name[1].lower() in user_groups][0]
        return System.objects.filter(dealer__name=dealer_name)
    return System.objects.all()

def get_serializer_class(self):
    user_groups = self.request.user.groups.all().values_list('name', flat=True)

    if 'x' not in user_groups:
        return ExternalSystemSerializer
    return SystemSerializer

I set up drf-spectacular using the docs. I added it to my INSTALLED_APPS in my settings.py and in my urls.

urlpatterns = [
# back
path('', include('api.urls')),
path('schema/', SpectacularAPIView.as_view(), name='schema'),
# Optional UI:
path('schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
path('schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
path('v1/', include('users.urls.v1', namespace='users')),]

All my endpoint are served under my-domain-name/v1/my-endpoint.

When I try to see my swagger It returns an empty dict as if I had no views in my project, however when I was using drf-yasg I could see all my endopints.

I tried to add this to my settings.py, same result.

SPECTACULAR_SETTINGS = { “SCHEMA_PATH_PREFIX”: “/v1” }

Where Am I doing wrong ?

Thank you

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

Hi @RomikimoR, from your examples i can’t really tell how/if you are using versioning but this is the problem in 99% of the cases where the schema is completely empty without warnings.

https://drf-spectacular.readthedocs.io/en/latest/faq.html#i-get-an-empty-schema-or-endpoints-are-missing

the schema view is unversioned by default and if all views are versioned, the schema is empty.

you can do 2 things:

  1. nest the schema endpoints under the v1/schema/, then the version should be used automatically (given the default versioning class is correct)
  2. explicitly provide version to schema views: SpectacularAPIView.as_view(api_version='v1')
  3. similar to 2., the CLI can also be provided with explicit version

good to hear.

I have used python and typescript-fetch targets for openapi-generator. It has worked very well for me. spectacular explicitly makes a few concessions so that code generators have an easier time. I have not used openapi-python-client personally, but I know that quite a few spectacular users do use it successfully. However, openapi-generator is of priority for spectacular, because it is the most widely used code generator.

For users that generate code from the schema, I highly recommend using the setting 'COMPONENT_SPLIT_REQUEST': True,. It leads to significantly better model code with less read-only/write-only issues.

Oops, just found my error, I had another VERSION set in SPECTACULAR_SETTINGS. I commented it out and now it works 😃 thanks for the help!

Thanks for the kind words.

  1. Subclassing the AutoSchema is usually a matter of last resort. I did quite a few complicated codebases and never needed to change the AutoSchema. You usually use the decorators first, most importantly @extend_schema(...) (example). The vast majority of issues can be resolved with that. For custom lib behavior, the extensions are usually the best fit. You can subclass AutoSchema if needed, but it is a lot less elegant and more error prone.

Please read up on https://drf-spectacular.readthedocs.io/en/latest/customization.html . Subclassing AutoSchema should only be needed after you exhausted all other steps.

  1. WS and custom token retrieval is outside the scope of spectacular. However, we do support multi parameter auth schemes via extension. This test should pretty much cover your use-case:

https://github.com/tfranzel/drf-spectacular/blob/c43b36476c00171b2c1d8dcb0e6fe05d6fe17c1f/tests/test_extensions.py#L128

I’m keep scratching my head over this one. I’m also getting empty schema without warnings. Possibly something with my path routing, but i can’t figure out what it is. Somebody that can maybe spot the error? I’m not using any versioning in my views.

urls.py

from django.urls import include, path
from rest_framework import routers
from . import views
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView

router = routers.DefaultRouter()
router.register(r'transactions', views.TransactionViewSet)
router.register(r'transaction', views.TransactionReadOnlyViewSet)
router.register(r'tokens', views.TokenViewSet)

urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('schema/', SpectacularAPIView.as_view(), name='schema'),
    # Optional UI:
    path('schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    path('schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),    

]

settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

SPECTACULAR_SETTINGS = {
    'TITLE': 'Your Project API',
    'DESCRIPTION': 'Your project description',
    'SERVE_INCLUDE_SCHEMA': True,
    # OTHER SETTINGS
}

Out put when running manage.py spectacular:

openapi: 3.0.3
info:
  title: Your Project API
  version: 0.0.0
  description: Your project description
paths: {}
components: {}

Update

Never mind I found the error. Since we are using django hosts, it’s important that the ROOT_URLCONF setting is set to the correct urls.py file. In our case, it was the wrong one, so drf-spectacular was not able to read the api urls.

Hi,

Ok I am sorry about opening an issur for such an easy problem I did not get that namespace are used for versioning.

Have a great day.