Doctrine ORM StateOptions uses entity instead of DTO class
API Platform version(s) affected: 3.2.7
Description
Hello There (General Kenwebby) ! I want to split my API logic from my entity logic using ORM state Options.
It works perfectly on a Get Operation (there is the "force_resource_class" specified in my AbstractItemNormalizer, but when using a GetCollection it does not work and serializes my entity object instead.
How to reproduce
Create an Entity "Club" and a DTO "ClubApi", set an id and a name property on the entity and an id only on the ClubApi Class.
Additional Context
When using Get Operation
When using GetCollection Operation
Thanks a lot !
https://github.com/api-platform/core/blob/main/tests/Fixtures/TestBundle/ApiResource/Issue5648/DummyResource.php#L25-L31 ? Not sure what you're trying to do force_resource_class is an internal thing not sure you should use this yourself...
Hi ! Sorry I was not clear. I'm not using force_resource_class, i was just showing the behavior in the AbstractItemNormalizer that I have in my debugger ! :-)
Here's my snippet for my resource !
#[ApiResource(
shortName: 'Club',
operations: [
new GetCollection(),
new Get()
],
stateOptions: new Options(entityClass: Club::class)
)]
class ClubApi
{
#[ApiProperty(identifier: true)]
public ?Uuid $id = null;
}
I have defined the entity like this, in the Get Operation i have the force_resource_class set in my context, but not in the GetCollection.
I have found out that Doctrine gets the whole object and not only the properties declared in my ClubApi Object. I don't get why. I am following the Symfonycasts tutorial on https://symfonycasts.com/screencast/api-platform-extending/state-options
The expected behavior in GetCollection is incorrect, where as when using Get it is correct. If you could help me that would be great ! :D
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hi! I can confirm this error on the latest 3.2.14 and have a reproducer from our tutorial:
The ApiResource is: https://github.com/SymfonyCasts/api-platform3/blob/entity-class-get-collection-reproducer/src/ApiResource/UserApi.php
#[ApiResource(
shortName: 'User',
stateOptions: new Options(entityClass: User::class),
)]
class UserApi
{
public ?int $id = null;
public ?string $email = null;
public ?string $username = null;
}
To reproduce:
git clone [email protected]:SymfonyCasts/api-platform3.git entity-class-get-collection-reproducer
cd entity-class-get-collection-reproducer
git checkout -b entity-class-get-collection-reproducer origin/entity-class-get-collection-reproducer
composer install
symfony serve
Then go to
- https://127.0.0.1:8000/api/users/1.jsonld (GOOD - it serializes the
UserApiclass) - https://127.0.0.1:8000/api/users.jsonld (BAD serializes the
Userclass)
I can also confirm that the bug is introduced when upgrading specifically from v3.1.14 => v3.1.15 https://github.com/api-platform/core/releases/tag/v3.1.15
Cheers!
sorry for the delay, thanks for the reproducer