django 验证Swagger API文档(drf-yasg)

6jjcrrmo  于 2023-04-22  发布在  Go
关注(0)|答案(2)|浏览(270)

我已经设置了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。
Am正在使用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和我自己的自定义权限类,但它们都失败了,并出现相同的错误。

r1wp621o

r1wp621o1#

原来问题是我在DRF的DEFAULT_AUTHENTICATION_CLASSES中丢失了rest_framework.authentication.SessionAuthentication
因此,通过Django Admin登录视图登录后发送的请求对象没有用户,因此所有权限类都失败,然后会导致上述错误。
因此,在添加它之后,drf-yasg显示了它所有的荣耀。

Bug:

当这种情况发生时,它抛出的错误被当作bug抛出。参见#issue58

whhtz7ly

whhtz7ly2#

如果只使用JWT,则应在模式视图中设置身份验证类:

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

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

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,
  )

它应该工作,并要求您设置您的用户名和密码。

相关问题