core icon indicating copy to clipboard operation
core copied to clipboard

Doctrine ORM StateOptions uses entity instead of DTO class

Open dynalouis opened this issue 2 years ago • 4 comments

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 image

When using GetCollection Operation image

Thanks a lot !

dynalouis avatar Dec 12 '23 16:12 dynalouis

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...

soyuka avatar Dec 13 '23 14:12 soyuka

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.

dynalouis avatar Dec 13 '23 14:12 dynalouis

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

dynalouis avatar Dec 20 '23 08:12 dynalouis

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.

stale[bot] avatar Feb 18 '24 09:02 stale[bot]

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 UserApi class)
  • https://127.0.0.1:8000/api/users.jsonld (BAD serializes the User class)

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!

weaverryan avatar Feb 22 '24 14:02 weaverryan

sorry for the delay, thanks for the reproducer

soyuka avatar Mar 29 '24 08:03 soyuka