API V2: Filtering nested fields via query parameter (Documents
&c)
#601
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Issue
This PR resolves issue #576 by implementing basic filtering on nested Serializers for V2 of the Open5e API.
Currently, on the
staging
branch there is no way to omit fields returned from nested serializers in the same way they can be omitted on root serializers.This is especially problematic when working with the
document
field returned by most serialized. In the vast majoiry of cases, the data that is required by this field at thename
,key
and perhapspermalink
. These fields can be requested simply enough when you hit the/documents
endpoint.But this kind of filtering is not possible when the serializer is accessed as a field on another serializer. This means that all document ForeignKeys will contain the full markdown copy describing the source as well as the full text for all of the licenses that are used. This leads to slower response times on the front-end by hugely overfetching
The Solution / What has changed?
The
GameContentSerializer
has been extended to recursively pass query parameters included in the request down to nested serializers via thecontext
property.Usage / Demonstration
url
.fields=name,document
. This will omit all fields in the API response except those specified in thefield
param. Note that thedocument
field returns a url (link)depth=1
to the url. All fields for the nesteddocument
serializer are returned. Note that doubly-nested fields such asgamesystem
andpublisher
return flat urls. (link)fields=
parameter with the nested field's name, seperated with a double-underscore. In this case:document__fields=name,gamesystem
(link)gamesystem
can be viewed by increasing thedepth
to 2documents__gamesystem__fields=name
(link)Other changes
The changes to the other serializer files were to make sure that the Document field is correctly configured on the other serializers to inherit from the
GameContentSerializer
.The changes made to
v2/views/spells.py
were made to preserve functionality of PR #592. Which previously had used theMeta
field to access thedepth
property. This is now accessed via therequest
object instead.Test are failing
This is beacuse that updating the Serializer has altered the representation of data that some endpoints return when no query parameters are added. These are simple fixes, but it felt a little permature to change the tests so that this PR pasted without first discussing with the other developer