Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty list for href and id in filters #4439

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
21 changes: 12 additions & 9 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)
Comment on lines -127 to +130
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this issue also apply when there is not a filter method provided? I'm now a bit confused by the link you provided to django-filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, anytime a filter method is used for an "in" filter, the issue would occur.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed a bug for the problem: carltongibson/django-filter#1609

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, when a filter method is NOT used, there is no such issue. It just calls the filter method on the filter without checking against EMPTY_VALUES.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



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 @@ -270,8 +278,8 @@ 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_id__in = IdInFilter(field_name="pk")
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