Skip to content

[BUGFIX] ?exclude query parameter now works on nested fields#919

Open
calumbell wants to merge 3 commits into
open5e:stagingfrom
calumbell:918/nested-exclude-query-param
Open

[BUGFIX] ?exclude query parameter now works on nested fields#919
calumbell wants to merge 3 commits into
open5e:stagingfrom
calumbell:918/nested-exclude-query-param

Conversation

@calumbell
Copy link
Copy Markdown
Contributor

Description

This PR fixes an issue with the ExcludeFieldsMixin where was not able to remove nested fields from API responses.

The specific use case that PR supports can be found in the following issue: open5e/open5e#865 - on the /v2/creatures endpoint there is no way currently of excluding the attacks sub-field of the actions field. This can now be achieved by:

/v2/creatures/srd_goblin/?actions__exclude=attacks

Not the use of the double-underscore operator mirrors the default DRF method of whitelisting nested fields via the ?fields query parameter.

The code is pretty knotty, requires a bit of parsing out. For this PR I have emphasised readability and gone slightly more verbose then I typically would. It think it serves the feature best.

ExcludeFieldsMixin now takes captures all of the query parameters from a request which end with "__excludes" along with exact matches with "exclude" and uses them as keys in a dict where the value for each is a list of fields to remove from that sub-field. The serializer is then recursively traversed and fields marked for exclusion are pruned.

During testing I noticed some weird interactions with the EagerLoadingMixin, slow response times and an increase in several orders of magnitude to the number of SQL queries made. Changes were made so that it played nicely with the ExcludeFieldsMixin. After the fact I thought that these changes had made the mixin difficult to read so I opted to refactor out some of the thorny one-liners into their own methods and generally improve the documentation.

Related Issue

Closes #918

How was this tested

  • Spot checked on local Django test server
  • All pytest tests are passing on local
  • CI / PR validation tests are all passing

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.

[BUG] exclude query parameter doesn't work for nested fields

1 participant