我已经设置了DRF-YASG,但无法弄清楚如何配置它以显示需要身份验证的视图。
下面是配置。
schema_view = get_schema_view(
openapi.Info(
title="Swagger Doc",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact@snippets.local"),
license=openapi.License(name="BSD License"),
),
validators=['flex', 'ssv'],
permission_classes=(permissions.AllowAny,), # If I change the permission it throws an exception. See below
public=False,
patterns=public_apis,
)
这些public_apis
是我希望人们在进行身份验证后看到的 API。
使用上述配置,它不会显示单个 API。它只显示Authorize
Button 和文字No operations defined in spec!
。但如果我更改public=False
为public=True
then 它会显示所有 API。
PS:之前我使用的是Django Rest Swagger,并且我能够将其配置为仅在提供 JWT 令牌后才显示 API。
我正在使用 JWT 进行身份验证。
权限变更例外:
另一个问题是,如果我将上面的权限更改为 DRF Permission 类,渲染将失败并出现以下错误:
Internal Server Error: /swagger/
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/usr/local/lib/python3.6/site-packages/django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/usr/local/lib/python3.6/site-packages/rest_framework/response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/usr/local/lib/python3.6/site-packages/drf_yasg/renderers.py", line 54, in render
self.set_context(renderer_context, swagger)
File "/usr/local/lib/python3.6/site-packages/drf_yasg/renderers.py", line 62, in set_context
renderer_context['title'] = swagger.info.title
AttributeError: 'dict' object has no attribute 'info'
Internal Server Error: /swagger/
我尝试将其更改为permmissions.IsAuthenticated
我自己的自定义权限类,但它们都失败并出现相同的错误。