验证Swagger API文档(drf-yasg)

13

我已经设置了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按钮和文本,该文本表示No operations defined in spec!。但如果我将public=False更改为public=True,则会显示所有API。
PS:以前我使用的是Django Rest Swagger,我已经能够将其配置为仅在提供了JWT令牌之后显示API。
我正在使用JWT进行身份验证。 更改权限时出现异常: 另一个问题是,如果我将上面的权限更改为DRF权限类,则呈现失败并显示以下错误:
  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和我的自定义权限类,但它们都会出现相同的错误。

2个回答

13

问题出在 DRF 的 DEFAULT_AUTHENTICATION_CLASSES 中缺少 rest_framework.authentication.SessionAuthentication.

所以通过 Django 管理员登录视图登录后发送的请求对象没有用户,因此所有权限类都会失败,然后导致上述错误。

添加后,drf-yasg 显示其全部功能。

错误信息:

然而,当这种情况发生时,它抛出的错误被认为是 bug。详情请参见#issue58.


0

如果你只使用JWT,那么你应该在模式视图中设置认证类:

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ],
}

所以你的模式视图应该是这样的:

  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"),
      ),
    authentication_classes=(authentication.BasicAuthentication,),
    public=True,
  )

它应该可以运行,并要求您设置用户名和密码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接