6

我正在我的项目中使用drf-yasg为 django rest 框架 API 实现 swagger。

我遵循 drf-yasg 的快速入门部分。当我点击 /swagger 时得到 AssertionError: duplicate Parameters found。下面是我的代码和回溯。

网址.py

from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      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"),
   ),
   public=True,
   permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    # path('graphql', csrf_exempt(GraphQLObservableUnboxingView.as_view(graphiql=True, schema=schema))),
]

追溯


```Traceback (most recent call last):
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/views.py", line 94, in get
    schema = generator.get_schema(request, self.public)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/generators.py", line 254, in get_schema
    paths, prefix = self.get_paths(endpoints, components, request, public)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/generators.py", line 412, in get_paths
    operation = self.get_operation(view, path, prefix, method, components, request)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/generators.py", line 454, in get_operation
    operation = view_inspector.get_operation(operation_keys)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/inspectors/view.py", line 36, in get_operation
    parameters = self.add_manual_parameters(parameters)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/inspectors/view.py", line 172, in add_manual_parameters
    return merge_params(parameters, manual_parameters)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/utils.py", line 282, in merge_params
    parameters = param_list_to_odict(parameters)
  File "/home/abc/development/sw_api/env_sw_api_python3.7/lib/python3.7/site-packages/drf_yasg/utils.py", line 267, in param_list_to_odict
    assert len(result) == len(parameters), "duplicate Parameters found"
AssertionError: duplicate Parameters found

4

1 回答 1

2

这可能是因为其中一个 APIview 具有重复的过滤器。就我而言,我有重复的搜索过滤器。它被启用,filter_backends = [filters.SearchFilter, DjangoFilterBackend] 并且它是一个字段/属性filterset_class = CustomerFilterSet

您可以通过注释掉 urlconfs(一个一个或一个批次)来找出导致崩溃的确切视图,以找出问题的根源,并检查过滤器。

于 2020-11-19T09:15:40.460 回答