core icon indicating copy to clipboard operation
core copied to clipboard

ObjectMapperProvider does not handle $data for requests with `pagination=false`

Open senaria opened this issue 2 months ago • 0 comments

API Platform version(s) affected: 4.2.6

Description

Using stateOptions with Map attribute does not work for collection endpoints serving data with pagination=false.

How to reproduce

#[GetCollection(
    paginationEnabled: false,
    paginationClientEnabled: false,
    stateOptions: new Options(entityClass: Log::class)
)]
#[Map(source: Log::class)]
class LogDto
{
    public function __construct(
        #[Map(source: 'username', transform: UserIdByUsernameTransformer::class)]
        public int $userId,
    )
    {}
}

Calling the collection endpoint will now fail the serialization process with Can't get a way to read the property "userId" in class "XXX\EntityLog"

Why? Because api/vendor/api-platform/core/src/State/Provider/ObjectMapperProvider.php:43 skips arrays and therefore also the mapping. Which therefore not ends with LogDto[] in the serialisation process but with Log[]

Possible Solution
I'll provide a fix+test.

The ObjectMapperProvider needs to consider also array data of non paginated collection responses. Since we check already for canMap, we should be able to allow arrays through. To guard we could even ensure before that its an collection operation, but that might be overkill.

Additional Context
Nope.

senaria avatar Nov 28 '25 12:11 senaria