Open
Description
API Platform version(s) affected
5.0.8 (also present on main)
Description
SparseFieldsetParameterProvider drops requested JSON:API fields in two cases, so moving from the deprecated Operation::$filters + PropertyFilter setup to new QueryParameter(filter: SparseFieldset::class) changes responses:
- The name converter is ignored. With
name_converter: serializer.name_converter.camel_case_to_snake_case, attributes are serialized asartist_credit, and clients sendfields[Release]=artist_credit. The provider compares the raw value against the PHP property names (artistCredit) within_array, so the field never matches and is dropped.PropertyFilterdenormalizes through the name converter. - Resources using
stateOptionsget the entity's properties.ParameterResourceMetadataCollectionFactoryfills aPropertiesAwareInterfaceparameter's properties fromgetStateOptionsClass(), i.e. the Doctrine entity behindstateOptions: new Options(entityClass: ...). The provider then only allows those for the primary type, so readable properties that exist only on the API resource (computed attributes) can never be selected.
How to reproduce
#[ApiResource(
stateOptions: new Options(entityClass: ReleaseEntity::class),
operations: [new GetCollection(parameters: [
'fields' => new QueryParameter(filter: SparseFieldset::class),
])],
)]
final class Release
{
public string $title;
public string $artistCredit; // entity has it too
public int $totalTracks; // computed, not on the entity
}GET /releases?fields[Release]=title,artist_credit,total_tracks with the snake_case name converter returns only title.
Possible solution
Denormalize each requested field through api_platform.name_converter before matching, and build the allowed properties for the primary type from the resource class (as getAllowedProperties() already does for included types) rather than the state-options class.
Metadata
Metadata
Assignees
Labels
No labels
Activity