Skip to content

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

@senaria

Description

@senaria

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions