Skip to content

Doctrine ORM StateOptions uses entity instead of DTO class #6039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dynalouis opened this issue Dec 12, 2023 · 6 comments · Fixed by #6255
Closed

Doctrine ORM StateOptions uses entity instead of DTO class #6039

dynalouis opened this issue Dec 12, 2023 · 6 comments · Fixed by #6255
Labels

Comments

@dynalouis
Copy link

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 !

@soyuka
Copy link
Member

soyuka commented Dec 13, 2023

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

@dynalouis
Copy link
Author

dynalouis commented Dec 13, 2023

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
Copy link
Author

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

Copy link

stale bot commented Feb 18, 2024

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 stale bot added the stale label Feb 18, 2024
@weaverryan
Copy link
Contributor

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

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!

@stale stale bot removed the stale label Feb 22, 2024
@soyuka soyuka added the bug label Mar 26, 2024
@soyuka
Copy link
Member

soyuka commented Mar 29, 2024

sorry for the delay, thanks for the reproducer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants