Skip to content

DjangoFilterBackend.get_filterset_kwargs doesn't modify data dict while iterating #580

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

Merged
merged 7 commits into from
Mar 2, 2019
Merged

DjangoFilterBackend.get_filterset_kwargs doesn't modify data dict while iterating #580

merged 7 commits into from
Mar 2, 2019

Conversation

mindblight
Copy link
Contributor

@mindblight mindblight commented Feb 24, 2019

It looks like DjangoFilterBackend.get_filterset_kwargs reused the same dictionary for the original (filter[something]) keys, the converted (something) keys, and the extra params (include or page). By calling data[key] = val in while iterating data.items(), the indexes can be incorrectly changed: https://stackoverflow.com/questions/6777485/modifying-a-python-dict-while-iterating-over-it

Example

With the path: /api/products?filter[organization]=44cbbad6-d418-4129-8093-731637d5013b&filter[archived]=False&filter[productType.key]=biomass&include=images,product_type, different filter params were ignored based on their order in the param string.

  • filter[productType.key]=...&filter[organization]=...&filter[archived]=False&include=images,product_type
    • ignores filter[archived]
  • filter[productType.key]=...&filter[organization]=...&filter[archived]=False&include=images,product_type
    • ignores filter[organization]
  • filter[archived]=False&filter[productType.key]=...&filter[organization]=...&include=images,product_type
    • ignores filter[organization]
  • filter[archived]=False&filter[organization]=...&filter[productType.key]=...&include=images,product_type
    • ignores filter[productType.key]
  • filter[organization]=...&filter[archived]=False&filter[productType.key]=...&include=images,product_type
    • ignores filter[productType.key]
  • filter[organization]=...&filter[productType.key]=...&filter[archived]=False&include=images,product_type
    • ignores filter[archived]
  • filter[organization]=...&filter[productType.key]=...&filter[archived]=False
    • works perfectly

Fixes #

Description of the Change

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name in AUTHORS

@mindblight mindblight changed the title switched DjangoFilterBackend.get_filterset_kwargs to populate new dat… DjangoFilterBackend.get_filterset_kwargs doesn't modify data dict while iterating Feb 24, 2019
@mindblight
Copy link
Contributor Author

The fix doesn't return other params in data. It didn't seem to have an impact, but I'd love to have someone who knows the code base comment.

@codecov
Copy link

codecov bot commented Feb 24, 2019

Codecov Report

Merging #580 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #580      +/-   ##
==========================================
- Coverage   95.64%   95.64%   -0.01%     
==========================================
  Files          55       55              
  Lines        2850     2849       -1     
==========================================
- Hits         2726     2725       -1     
  Misses        124      124
Impacted Files Coverage Δ
rest_framework_json_api/django_filters/backends.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 75e2f82...b478f06. Read the comment docs.

@codecov
Copy link

codecov bot commented Feb 24, 2019

Codecov Report

Merging #580 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #580      +/-   ##
==========================================
+ Coverage   95.64%   95.65%   +<.01%     
==========================================
  Files          55       55              
  Lines        2850     2856       +6     
==========================================
+ Hits         2726     2732       +6     
  Misses        124      124
Impacted Files Coverage Δ
rest_framework_json_api/django_filters/backends.py 100% <100%> (ø) ⬆️
example/tests/test_filters.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30ca94b...8eca777. Read the comment docs.

Copy link
Contributor

@n2ygk n2ygk left a comment

Choose a reason for hiding this comment

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

Thanks for catching my bug!

@sliverc can you take a second look just to make sure?

@n2ygk n2ygk requested a review from sliverc February 24, 2019 14:26
Copy link
Member

@sliverc sliverc left a comment

Choose a reason for hiding this comment

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

@mindblight
Thanks for your pull requests. The change looks good!
To ensure this fix for the future could you add a test reproducing the issue?
A changelog entry would be good as well and add yourself to AUTHORS if you like. Thanks.

Arion Sprague added 3 commits February 25, 2019 10:53
…ed-filter-param

* upstream/master:
  Clarify docs on controlling compound documents (#579)
  Update isort from 4.3.4 to 4.3.5 (#581)
@mindblight
Copy link
Contributor Author

@sliverc Done! It seems like 4 params is the magic number for this bug to show up.

@mindblight
Copy link
Contributor Author

@sliverc, It looks like the codecov checks are hanging for some reason. Is there any way to re-trigger those, or merge without them?

@sliverc sliverc merged commit 272a752 into django-json-api:master Mar 2, 2019
@sliverc
Copy link
Member

sliverc commented Mar 2, 2019

@mindblight Thanks. Merged.

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.

3 participants