DRF 壮观未发现自定义身份验证扩展 类
DRF spectacular not discovering custom auth extension classes
当从 rest_framework_simplejwt.authentication.JWTAuthentication drf-spectacular swagger-ui 扩展新的令牌身份验证 class 时,授权按钮消失并且无法添加令牌持有者,我想当你子class 出错了。
重现步骤:
首先,创建一个 Django 项目,其中安装了 rest 框架和 drf-spectacular 以及简单的 jwt,并配置了文档 guidance。到达 /swagger-ui/ 并且工作正常。
然后像下面这样创建 JWTAuthentication 的子 class:
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/,e 是没有授权按钮的!!!我该如何解决这个问题?
我什至尝试创建一个 AuthenticationExtension,例如:
from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
class SimpleJWTTokenUserScheme(SimpleJWTScheme):
target_class = 'path_to_your_module.JWTAuthentication'
但是没有办法在任何地方注册它,无论是在互联网上还是在文档中!
覆盖身份验证时如何修复授权按钮 class??
编辑:做 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?
更新 1
来自文档 Where should I put my extensions? / my extensions are not detected
The extensions register themselves automatically. Just be sure that
the python interpreter sees them at least once. To that end, we
suggest creating a PROJECT/schema.py
file and importing it in your
PROJECT/__init__.py
(same directory as settings.py
and urls.py
)
with import PROJECT.schema
. Please do not import the file in
settings.py
as this may potentially lead to cyclic import issues.
原答案
这似乎是软件包本身的错误。您可以使用 actual class 而不是 到 class 的路径扩展 auth 扩展
from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
<b>from path.to.custom.jwt.auth import JWTAuthentication</b>
class SimpleJWTTokenUserScheme(SimpleJWTScheme):
target_class = <b>JWTAuthentication</b>
我在这里创建了一个简单的例子,drf-spectacular-example
,希望有人能从中受益!!!
当从 rest_framework_simplejwt.authentication.JWTAuthentication drf-spectacular swagger-ui 扩展新的令牌身份验证 class 时,授权按钮消失并且无法添加令牌持有者,我想当你子class 出错了。
重现步骤:
首先,创建一个 Django 项目,其中安装了 rest 框架和 drf-spectacular 以及简单的 jwt,并配置了文档 guidance。到达 /swagger-ui/ 并且工作正常。
然后像下面这样创建 JWTAuthentication 的子 class:
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/,e 是没有授权按钮的!!!我该如何解决这个问题?
我什至尝试创建一个 AuthenticationExtension,例如:
from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
class SimpleJWTTokenUserScheme(SimpleJWTScheme):
target_class = 'path_to_your_module.JWTAuthentication'
但是没有办法在任何地方注册它,无论是在互联网上还是在文档中!
覆盖身份验证时如何修复授权按钮 class??
编辑:做 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?
更新 1
来自文档 Where should I put my extensions? / my extensions are not detected
The extensions register themselves automatically. Just be sure that the python interpreter sees them at least once. To that end, we suggest creating a
PROJECT/schema.py
file and importing it in yourPROJECT/__init__.py
(same directory assettings.py
andurls.py
) withimport PROJECT.schema
. Please do not import the file insettings.py
as this may potentially lead to cyclic import issues.
原答案
这似乎是软件包本身的错误。您可以使用 actual class 而不是 到 class 的路径扩展 auth 扩展
from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
<b>from path.to.custom.jwt.auth import JWTAuthentication</b>
class SimpleJWTTokenUserScheme(SimpleJWTScheme):
target_class = <b>JWTAuthentication</b>
我在这里创建了一个简单的例子,drf-spectacular-example
,希望有人能从中受益!!!