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)
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:
v1/schema/, then the version should be used automatically (given the default versioning class is correct)SpectacularAPIView.as_view(api_version='v1')good to hear.
I have used
pythonandtypescript-fetchtargets foropenapi-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 usedopenapi-python-clientpersonally, but I know that quite a few spectacular users do use it successfully. However,openapi-generatoris 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
VERSIONset inSPECTACULAR_SETTINGS. I commented it out and now it works 😃 thanks for the help!Thanks for the kind words.
AutoSchemais usually a matter of last resort. I did quite a few complicated codebases and never needed to change theAutoSchema. 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 subclassAutoSchemaif 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
AutoSchemashould only be needed after you exhausted all other steps.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
settings.py
Out put when running manage.py spectacular:
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.