获取“Assert错误:当我点击/swagger url时”发现重复参数

qzlgjiam  于 2023-01-02  发布在  其他
关注(0)|答案(2)|浏览(195)

我正在我的项目中使用drf-yasg for django rest框架API实现swagger。
我按照drf-yasg的快速启动部分进行操作,当我点击/swagger时,会得到AssertionError:发现重复的参数。2下面是我的代码和回溯。

    • 网址. 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
bsxbgnwa

bsxbgnwa1#

发生这种情况的原因可能是某个APIviews具有重复的筛选器。在我的示例中,我复制了搜索筛选器。它在filter_backends = [filters.SearchFilter, DjangoFilterBackend]中启用,并且是filterset_class = CustomerFilterSet中的字段/属性
您可以尝试通过注解掉urlconfs(一个接一个或批量)来找出问题的根源,并检查过滤器,从而找出导致崩溃的确切视图。

6vl6ewon

6vl6ewon2#

我在用某种方式实现Django分页时也遇到过类似的问题。使用***page_query_param***解决了这个问题!

class PublicationPagination(PageNumberPagination):
    page_size = 20       

    #line causing us trouble 
    page_size_query_param = 'page'

正确的用法在下面。

class PublicationPagination(PageNumberPagination):
page_size = 20

#notice the difference
page_query_param = 'page'

相关问题