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

JSON:API support for Django REST framework
https://django-rest-framework-json-api.readthedocs.org/
BSD 2-Clause "Simplified" License
1.19k stars 295 forks source link

Filter validation too rigid for filters with suffixes #938

Open SafaAlfulaij opened 3 years ago

SafaAlfulaij commented 3 years ago

Here: https://github.com/django-json-api/django-rest-framework-json-api/blob/fbb4b033a483b7edb62de3377c83a046a55f69b2/rest_framework_json_api/django_filters/backends.py#L80

RangeFilter is used to specify a value of the two ends without specifying the other. It works by looking for "min/after" and "max/before" in the query param.

This should work:

Using DateFromToRangeFilter, defined this way:

last_updated = DateFromToRangeFilter()

and passing the filter: filters[last_updated_after]=2016-01-01

will execute: .filter(last_updated__gte="2016-01-01")

Currently it fails as last_updated_after != last_updated (as in the quoted code: base_filters contains last_updated but not last_updated_after or last_updated_before)

sliverc commented 3 years ago

I agree this is a bug and this should actually work. There are also other suffixes used like min and max which should work too. Any there could be custom suffixes as well. So I wonder instead of validating those filters on our own, whether there is a easy way to let the filter class validate the filters.

eloi-martinez-qida commented 2 years ago

Yes, it looks like it's a duplicate, sorry for opining another issue.

Are there any plans to fix it? This issue has been open for more than a year.

sliverc commented 2 years ago

@eloi-martinez-qida No worries. Welcome to DJA! This is an OSS project with only volunteer contributors. So people work on the issues which are most relevant to them.

Therefore, if you are affected by this issue, I encourage you to work on a PR. Happy to assist if needed.

eloi-martinez-qida commented 2 years ago

@sliverc What would be the best way to tackle the problem?

sliverc commented 2 years ago

The best way would be to check whether the django filter class has a way to validate the filter names.