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

Conversation

daviddavis
Copy link
Contributor

@daviddavis daviddavis commented Sep 18, 2023

Fix bug where empty list for the pulp_href__in and pulp_id__in filters return all results instead of no results. The problem is that when using FilterMethod, django-filter just simply ignores 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 #4437

@daviddavis daviddavis marked this pull request as draft September 18, 2023 14:20
@daviddavis daviddavis marked this pull request as ready for review September 18, 2023 16:07
@gerrod3
Copy link
Contributor

gerrod3 commented Sep 18, 2023

Nice fix! Can you add a check for empty lists to the test_pulp_id_href_filter in test_filter.py?

@daviddavis daviddavis changed the title Handle empty list for pulp_href__in filter Handle empty list for href and id in filters Sep 18, 2023
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
Comment on lines -127 to +130
pass
def filter(self, qs, value):
if value is None:
return qs
return qs.filter(pk__in=value)
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.

@dralley
Copy link
Contributor

dralley commented Sep 20, 2023

Does this need backports? And how far back?

@daviddavis
Copy link
Contributor Author

We requested this feature so I wouldn't be surprised if we're the only ones using these filters. Moreover, it's an edge case that I am not sure many users will hit. That said, it's been available in pulp since 3.24.0.

@daviddavis
Copy link
Contributor Author

For us though, we're still on an older version of pulpcore (until pulp/pulp_deb#295 is merged) and we've patched our version.

@dralley dralley merged commit 2032e64 into pulp:main Sep 20, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pulp_href__in filter doesn't handle empty set properly
4 participants