Getting "AssertionError: duplicate Parameters found" when I hit /swagger url
Asked Answered
S

3

8

I am implementing swagger using drf-yasg for django rest framework APIs in my project.

I follow the quick start section of drf-yasg. When I hit /swagger gets AssertionError: duplicate Parameters found. Below is my code and traceback.

urls.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="[email protected]"),
      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


```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

Sphygmo answered 3/7, 2020 at 15:4 Comment(3)
I am running into the same issue. What version of django, djangorestframework and drf_yasg are you using?Non
Same issue showing up todayPictor
django3.0.6 djangorestframework3.10.3 drf-yasg1.17.1Sphygmo
S
6

This probably happens because one of the APIviews has duplicate filters. In my case I had search filter duplicated. It was enabled in filter_backends = [filters.SearchFilter, DjangoFilterBackend] as well as it was a field/attribute in filterset_class = CustomerFilterSet

You can try to figure out which exact view is causing the crash by commenting out urlconfs (one by one or in batches) to pin dow the source of the problem, and check filters.

Sivas answered 19/11, 2020 at 9:15 Comment(0)
S
0

Similar problem occured to me while implementing Django pagination in a certain way. Using page_query_param solved the problem!

class PublicationPagination(PageNumberPagination):
    page_size = 20       

    #line causing us trouble 
    page_size_query_param = 'page'

Proper usage down below.

class PublicationPagination(PageNumberPagination):
    page_size = 20

    #notice the difference
    page_query_param = 'page'
Shirtmaker answered 30/12, 2022 at 12:47 Comment(0)
F
0

I had the same problem and it because I implemented this:

class ApprobationReportTripViewSet(TripQuerySetReportMixin, viewsets.ReadOnlyModelViewSet):
  serializer_class = ApprobationReportTripListSerializer
  serializer_action_classes = {
    'list': ApprobationReportTripListSerializer,
    'retrieve': ApprobationReportTripDetailSerializer
  }
  filter_backends = (filters.OrderingFilter, DjangoFilterBackend)

Into the DjangoFilterBackend exist an implementation of filters.OrderingFilter and that generated my problem, the solution was replace filter_backends = (filters.OrderingFilter, DjangoFilterBackend) to filter_backends = (DjangoFilterBackend, )

Filiano answered 21/2, 2023 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.