django-json-api/django-rest-framework-json-api

Missing group type in custom QueryParamsValidationFilter

paweenwat-man opened this issue · 3 comments

Description of the Bug Report

I want to create my custom QueryParameterValidationFilter, according to this documentation
https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#filter-backends

Installed apps

  • Django REST Framework 3.14.0
  • Django REST Framework JSON API 6.0.0

How to reproduce

  1. Create custom QueryParamsValidationFilter from this doc https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#filter-backends
import re
from rest_framework_json_api.filters import QueryParameterValidationFilter

class MyQPValidator(QueryParameterValidationFilter):
    query_regex = re.compile(r'^(sort|include|show_all)$|^(filter|fields|page)(\[[\w\.\-]+\])?$')

  1. Use it as filter backend into my viewset
class SatelliteCategoryViewSet(PaginateByMaxMixin, viewsets.ModelViewSet):
  queryset = SatelliteCategory.objects.all()
  serializer_class = SatelliteCategorySerializer
  permission_classes = [permissions.AllowAny]
  ordering_fields = '__all__'
  filter_backends = [MyQPValidator]

  filterset_fields = {
    'norad': ('in', 'exact', 'contains'),
    'name': ('exact', 'icontains', 'iexact', 'contains',),
  }

  search_fields = ('norad', 'name',)

  def get_queryset(self):
    queryset = super().get_queryset()
    conjunction_pk = self.kwargs.get('conjunction_pk')
    if conjunction_pk is not None:
      queryset = queryset.filter(conjunction__pk=conjunction_pk)

    show_all = self.request.query_params.get('show_all', '')
    if show_all.lower() == 'true':
      self.pagination_class = None

    return queryset
  1. Open this viewset with browser (Must include query params with provided in Query Validation regular expression)
    image

Checklist

  • Code snippet to reproduce an error
  • Django REST Framework Error Image

Thanks for reporting. What Django REST framework JSON API version are you running? The stacktrace seems to be a bit off, so hard to tell what goes wrong here. If it is not the latest 6.0.0 could you try to reproduce it with the newest DJA version and share the stack trace again? Thanks.

I already used Django REST Framework JSON API 6.0.0. I forgot to include the version of DJA, I'm so sorry. I will edit the post and include the version.

Great thanks for the clarification. It seems the file changed in master since the last release and therefore the stacktrace did not match up.

The documentation seems to be outdated, in the regex there needs to be a group type otherwise it does not work.

The regex should look like the following (adjust it as you need it):

r"^(sort|include)$|^(?P<type>filter|fields|page)(\[[\w\.\-]+\])?$"

Does this work for you? If yes, a PR is welcome which updates the docs.