[13.x] Honor JSON:API sparse fieldsets for relationships#60897
Open
yazansalhi wants to merge 1 commit into
Open
[13.x] Honor JSON:API sparse fieldsets for relationships#60897yazansalhi wants to merge 1 commit into
yazansalhi wants to merge 1 commit into
Conversation
Sparse fieldsets were only applied to a resource object's attributes, so a request such as ?include=posts&fields[users]=name still returned the posts relationship on the users resource. The JSON:API specification defines a resource object's attributes and its relationships collectively as its fields, and states that when a client requests a restricted set of fields for a resource type, the server must not include additional fields in resource objects of that type. Apply the fieldset to relationships as well. Included resources are unaffected, since those are governed by the include parameter rather than by fields.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Sparse fieldsets are currently applied only to a resource object's
attributes. Relationships are not filtered, so a request that restricts the fields for a type still receives that type's relationships:{ "data": { "id": "1", "type": "users", "attributes": { "name": "Taylor" }, "relationships": { "posts": { "data": [{ "id": "1", "type": "posts" }] } } }, "included": [ ... ] }postswas not requested infields[users], so it should not appear.The specification defines a resource object's attributes and relationships collectively as its fields:
and requires that a restricted fieldset be respected:
This applies the fieldset to relationships using the same
usesRequestQueryString/hasSparseFieldsetchecks already used for attributes, soignoreFieldsAndIncludesInQueryString()continues to opt out of both, and an empty fieldset (fields[users]=) drops relationships just as #59813 made it drop attributes.After the change the response is:
{ "data": { "id": "1", "type": "users", "attributes": { "name": "Taylor" } }, "included": [ ... ] }Requesting the relationship keeps it, as expected:
Scope notes
includedis deliberately untouched. It is governed by theincludeparameter, not byfields, so the related resources are still returned (with their own type's fieldset applied to them).resolveResourceRelationshipIdentifiers()takes the resource type as an optional second parameter rather than a required one, so any application overriding thisprotectedmethod keeps working on 13.x.resolveResourceObject()passes the type it has already resolved, so there is no extra work per resource.Tests
Two tests added to
JsonApiResourceTest: one asserting an unrequested relationship is omitted, one asserting a requested relationship is retained. I confirmed the first fails without the source change and passes with it.Locally:
tests/Httpandtests/Integration/Httppass (770 tests, 2358 assertions), and Pint and PHPStan (phpstan.src.neon.dist) both report no issues.