Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

with drf-yasg, how to supply patterns?

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.

like image 556
jupiar Avatar asked Dec 17 '18 15:12

jupiar


People also ask

What is DRF-yasg?

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.

Is Django rest swagger deprecated?

Django REST Swagger: deprecated (2019-06-04) This project is no longer being maintained.


Video Answer


1 Answers

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

like image 76
Ken4scholars Avatar answered Oct 15 '22 06:10

Ken4scholars