I have installed drf-yasg, and it's working great. The problem I've got is that it's a big app, and has a huge amount of endpoints for each type of frontend client, i.e. /admin/v1
, /app/v1
, ...
So I thought it would be a good idea to separate documentation for each type, i.e
urlpatterns += [
url(r'^/admin/swagger/$', admin_schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
url(r'^/app/swagger/$', app_schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
So it looks as though drf-yasg supports this, via supplying patterns
into the get_scheme_view
:
admin_schema_view = get_schema_view(
openapi.Info(
title="API",
default_version='v1',
description="The set of API endpoints used.",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact@me"),
license=openapi.License(name="BSD License"),
),
patterns=?????,
validators=['flex', 'ssv'],
public=True,
permission_classes=(permissions.AllowAny,),
)
Now my guess was to supply a string, the same way as the first string when defining urls
, such as patterns=r'^admin/v1/',
which results in:
File "/usr/local/lib/python3.6/dist- packages/rest_framework/compat.py", line 55, in get_original_route
return urlpattern.regex.pattern
AttributeError: 'str' object has no attribute 'regex'
So with the documentation at drf-yasg docs:
patterns – if given, only these patterns will be enumerated for inclusion in the API spec
Exactly what type of object is needed here in order for to process patterns
? I've tried looking around the django-rest-framework and Django source code on github, but couldn't find what type is actually needed, they are both very large projects.
drf-yasg - Yet another Swagger generator. Generate real Swagger/OpenAPI 2.0 specifications from a Django Rest Framework API. Compatible with. Django Rest Framework: 3.10, 3.11, 3.12.
Django REST Swagger: deprecated (2019-06-04) This project is no longer being maintained.
After some experiments I found out the patterns it is expecting is a list of url patterns and not just a standard Python regex string. It should exactly be the same standard django urlpatterns
in urls.py
.
So assuming you have imported your admin API url patterns as admin_urlpatterns
, all you need is specify it in the patterns option
admin_schema_view = get_schema_view(
openapi.Info(
title="API",
default_version='v1',
description="The set of API endpoints used.",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact@me"),
license=openapi.License(name="BSD License"),
),
patterns=admin_urlpatterns,
validators=['flex', 'ssv'],
public=True,
permission_classes=(permissions.AllowAny,),
)
It was definitely difficult to find any example and they may want to include a vivid example in the docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With