Skip to content

Commit

Permalink
Handle empty list for href and id in filter
Browse files Browse the repository at this point in the history
Fix bug where empty list for the pulp_href__in or pulp_id__in filters
return all results instead of no results.

The problem is that when using FilterMethod, django-filter just simply
ignores None and empty set and returns the full list of results:

https://github.com/carltongibson/django-filter/blob/e4a70a667a0bf3882fd44b557bc76583d2c65cd1/django_filters/filters.py#L804-L805

There's no way to update the method to prevent this since this logic
happens before the method is called. Instead, this change moves the
method to the filter and adds a None check.

fixes pulp#4437
  • Loading branch information
daviddavis committed Sep 18, 2023
1 parent fda9645 commit 6fcdf28
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES/4437.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed bug where supplying an empty list for ``pulp_href__in`` or ``pulp_id__in`` would return all
results instead of none.
19 changes: 11 additions & 8 deletions pulpcore/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,19 @@ def filter(self, qs, value):


class IdInFilter(BaseInFilter, filters.UUIDFilter):
pass
def filter(self, qs, value):
if value is None:
return qs
return qs.filter(pk__in=value)


class HREFInFilter(BaseInFilter, filters.CharFilter):
pass
def filter(self, qs, value):
if value is None:
return qs

pks = [extract_pk(href) for href in value]
return qs.filter(pk__in=pks)


class PulpTypeFilter(filters.ChoiceFilter):
Expand Down Expand Up @@ -271,7 +279,7 @@ class BaseFilterSet(filterset.FilterSet):

help_text = {}
pulp_id__in = IdInFilter(field_name="pk", lookup_expr="in")
pulp_href__in = HREFInFilter(field_name="pk", method="filter_pulp_href")
pulp_href__in = HREFInFilter(field_name="pk")
q = ExpressionFilter()

FILTER_DEFAULTS = {
Expand Down Expand Up @@ -307,11 +315,6 @@ class BaseFilterSet(filterset.FilterSet):
"ne": _("not equal to"),
}

def filter_pulp_href(self, queryset, name, value):
# Convert each href to a pk
pks = [extract_pk(href) for href in value]
return queryset.filter(pk__in=pks)

@classmethod
def get_filters(cls):
filters = super().get_filters()
Expand Down
4 changes: 4 additions & 0 deletions pulpcore/tests/functional/api/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def test_pulp_id_href_filter(
redi_results = redirect_contentguard_api_client.list(**{filter: rbac_sample})
assert redi_results.count == 0

# test out empty list
redi_results = redirect_contentguard_api_client.list(**{filter: []})
assert redi_results.count == 0

# Test that filter fails when not a valid type
with pytest.raises(ApiException) as exc:
content_guards_api_client.list(**{filter: ["hello"]})
Expand Down

0 comments on commit 6fcdf28

Please sign in to comment.