【问题标题】:DRF spectacular not discovering custom auth extension classesDRF 壮观没有发现自定义身份验证扩展类
【发布时间】:2021-05-17 09:44:08
【问题描述】:

当从 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?

【问题讨论】:

    标签: django django-rest-framework swagger django-rest-framework-simplejwt drf-spectacular


    【解决方案1】:

    更新-1

    来自文档Where should I put my extensions? / my extensions are not detected

    扩展会自动注册。只要确定 python 解释器至少看到它们一次。为此,我们 建议创建一个PROJECT/schema.py 文件并将其导入您的 PROJECT/__init__.py(与settings.pyurls.py 相同的目录) 与import PROJECT.schema请不要将文件导入 settings.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,希望有人从中受益!!!

    【讨论】:

    • 好的,我猜第一个答案是错误的,因为导入问题。现在好了。
    • 第一个答案?我删除的那个?是的,这是错误的。但是这两种方法都可以
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2020-04-01
    • 2022-12-10
    • 2011-07-25
    • 2021-12-06
    • 2016-07-16
    • 1970-01-01
    相关资源
    最近更新 更多