feat: add per-type API transformer sparse fieldsets#10285
Open
michalsn wants to merge 1 commit into
Open
Conversation
datamweb
reviewed
Jun 8, 2026
| protected mixed $resource = null; | ||
|
|
||
| public function __construct( | ||
| private ?IncomingRequest $request = null, |
Contributor
There was a problem hiding this comment.
To allow reusing the same transformer for multiple relations (like posts and comments) with independent fieldsets, we should add an $overrideResourceType parameter to the constructor.
Comment on lines
+102
to
+132
| /** | ||
| * Resolves the requested field list for this transformer from the request. | ||
| * | ||
| * Supports both the flat `?fields=a,b` form and the per-type sparse | ||
| * fieldset form `?fields[<type>]=a,b`. The flat form is only honored when | ||
| * $allowFlat is true (i.e. for the root transformer); a type-specific | ||
| * fieldset is matched against this transformer's $resourceType at any | ||
| * nesting level. | ||
| * | ||
| * @return list<string>|null | ||
| */ | ||
| private function resolveFields(bool $allowFlat): ?array | ||
| { | ||
| $fields = $this->request->getGet('fields'); | ||
|
|
||
| // Sparse fieldsets: ?fields[posts]=id,slug -> ['posts' => 'id,slug'] | ||
| if (is_array($fields)) { | ||
| $scoped = ($this->resourceType !== null && is_string($fields[$this->resourceType] ?? null)) | ||
| ? $fields[$this->resourceType] | ||
| : null; | ||
|
|
||
| return $scoped !== null ? $this->splitList($scoped) : null; | ||
| } | ||
|
|
||
| // Flat fieldset: ?fields=id,slug (applies to the root only) | ||
| if ($allowFlat && is_string($fields)) { | ||
| return $this->splitList($fields); | ||
| } | ||
|
|
||
| return null; | ||
| } |
Contributor
There was a problem hiding this comment.
In resolveFields(), mixing fields[x]=... and fields=... in the URL causes PHP to natively overwrite the array and break the filters,we need to manually parse the QUERY_STRING to prevent this bug.
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.
Description
This PR adds per-type sparse fieldsets for API transformers via
fields[<type>].Transformers can opt in by defining
$resourceType, allowing nested resources to apply their own field filters without inheriting the rootfieldsparameter.Ref: #10278 (comment)
Checklist: