Yos*_*SaL 6 django swagger django-rest-framework django-rest-framework-simplejwt drf-spectacular
当从rest_framework_simplejwt.authentication.JWTAuthentication drf-spectaulous swagger-ui 授权按钮扩展新的令牌身份验证类时,授权按钮消失并且无法添加令牌承载者,我想当您子类化时它会出错。
重现步骤:
首先,创建一个 Django 项目,其中安装有 Rest Framework 和 drf-spectaulous 以及简单的 jwt,并按照文档指导进行配置。到达 /swagger-ui/ ,它工作正常。
然后创建 JWTAuthentication 的子类,如下所示:
from rest_framework_simplejwt.authentication import JWTAuthentication as JWTA
class JWTAuthentication(JWTA):
pass
Run Code Online (Sandbox Code Playgroud)
并在您的设置中:
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
'DEFAULT_AUTHENTICATION_CLASSES': (
'path_to_your_module.JWTAuthentication',
),
}
Run Code Online (Sandbox Code Playgroud)
现在,如果您转到 /swagger-ui/ ,则没有授权按钮!!! 我怎样才能解决这个问题?
我什至尝试创建一个 AuthenticationExtension ,例如:
from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
class SimpleJWTTokenUserScheme(SimpleJWTScheme):
target_class = 'path_to_your_module.JWTAuthentication'
Run Code Online (Sandbox Code Playgroud)
但无法在任何地方、互联网上或文档中注册它!覆盖身份验证类时如何修复授权按钮?
编辑:按照 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',
),
}
Run Code Online (Sandbox Code Playgroud)
引发异常:
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?
Run Code Online (Sandbox Code Playgroud)
扩展会自动注册。只要确保 python 解释器至少看到它们一次即可。为此,我们建议创建一个
PROJECT/schema.py文件并将其导入到您的 (与和PROJECT/__init__.py相同的目录)中,并使用. 请不要导入该文件, 因为这可能会导致循环导入问题。settings.pyurls.pyimport PROJECT.schemasettings.py
这似乎是包本身的错误。在扩展身份验证扩展时,您可以使用实际的类而不是类的路径
from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
from path.to.custom.jwt.auth import JWTAuthentication
class SimpleJWTTokenUserScheme(SimpleJWTScheme):
target_class = JWTAuthenticationRun Code Online (Sandbox Code Playgroud)
我在这里创建了一个简单的例子,drf-spectacular-example希望有人能从中受益!