2

当从 rest_framework_simplejwt.authentication.JWTAuthentication drf-spectacular swagger-ui 扩展一个新的令牌认证类时,授权按钮消失并且没有办法添加令牌持有者,我猜当你子类化它时会出错。
重现步骤:
首先,创建一个 Django 项目,其中安装了 rest 框架和 drf-spectacular 和简单的 jwt,并使用文档指导进行配置。到 /swagger-ui/ 它工作正常。
然后创建一个 JWTAuthentication 的子类,如下所示:

from rest_framework_simplejwt.authentication import JWTAuthentication as JWTA

class JWTAuthentication(JWTA):
    pass

并在您的设置中:

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'path_to_your_module.JWTAuthentication',
    ),
}

现在如果你去 /swagger-ui/ 那里没有授权按钮!!!我怎样才能解决这个问题?
我什至尝试创建一个 AuthenticationExtension,例如:

from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme

class SimpleJWTTokenUserScheme(SimpleJWTScheme):
    target_class = 'path_to_your_module.JWTAuthentication'

但是没有办法在任何地方、互联网或文档中注册它!覆盖身份验证类时如何修复授权按钮?
编辑:做 JPG 所说的并在设置中导入扩展名:

# settings.py
from path.to.custom.extension import SimpleJWTTokenUserScheme
REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'path_to_your_module.JWTAuthentication',
    ),
}

引发异常:

  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/views.py", line 67, in get
    return self._get_schema_response(request)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/views.py", line 74, in _get_schema_response
    return Response(generator.get_schema(request=request, public=self.serve_public))
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/generators.py", line 250, in get_schema
    paths=self.parse(request, public),
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/generators.py", line 218, in parse
    assert isinstance(view.schema, AutoSchema), (
AssertionError: Incompatible AutoSchema used on View <class 'drf_spectacular.views.SpectacularAPIView'>. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?
4

1 回答 1

2

更新 1

从文档我应该把我的扩展放在哪里?/ 未检测到我的扩展程序

扩展程序会自动注册。只要确保 python 解释器至少看到它们一次。为此,我们建议创建一个PROJECT/schema.py文件并将其导入到您的 (与和PROJECT/__init__.py相同的目录)中。请不要导入文件, 因为这可能会导致循环导入问题。settings.pyurls.pyimport PROJECT.schemasettings.py


原始答案

这似乎是包本身的一个错误。在扩展auth 扩展时,您可以使用实际的类而不是类的路径

from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
from path.to.custom.jwt.auth import JWTAuthentication

class SimpleJWTTokenUserScheme(SimpleJWTScheme):
    target_class = JWTAuthentication

我在这里创建了一个简单的例子drf-spectacular-example,希望有人能从中受益!!!

于 2021-05-20T11:08:33.263 回答