core icon indicating copy to clipboard operation
core copied to clipboard

LinksHandlerTrait : The class "App\Entity\EntityName" cannot be retrieved from "App\Entity\EntityName"

Open xterr opened this issue 2 years ago • 4 comments

API Platform version(s) affected: 3.2.7

Description
RuntimeException is thrown on line 87 in LinksHandlerTrait for:

  1. Entity with a column of type enum
  2. Int BackedEnum, with a member with value = 0
  • The issue is present only when there is a value equal to 0 in the database.
  • The issue happens for single items and collections.
  • For non-enum fields value=0 is accepted and all functionality is as expected.

How to reproduce

enum StateEnum: int {
  case NEW      = 0;
  case ACTIVE   = 1;
  case INACTIVE = 2;
}

#[ORM\Entity]
#[ApiResource]
class Entity {
  #[ORM\Column(type: Types::SMALLINT, enumType: StateEnum::class)]
  #[Groups(['entity:read', 'entity:write'])]
  #[Assert\Choice(callback: [StateEnum::class, 'cases'])]
  state: StateEnum
}

Additional context After some debugging, I observed that the data was correctly retrieved from the database. The problem might start from ApiPlatform\GraphQl\State\Provider\ReadProvider#107 ReadProvider

xterr avatar Dec 20 '23 00:12 xterr

Are you using the enum as identifier? this isn't supported may we see the whole resource or some more informations as this is not enough?

soyuka avatar Dec 20 '23 09:12 soyuka

@soyuka no. The entity has id autoincrement as the identifier.

Only normal fields typed with enum (like in the example) have this problem. (I also have setters and getters defined as i know it impacts propertyResolver)

It seems to occur only for graphql endpoint.

I also did more tests by changing the column name from state to type and other non-common names), same problem.

Thank you 👍

xterr avatar Dec 20 '23 11:12 xterr

I see, can you provide a reproducer ?

soyuka avatar Dec 20 '23 11:12 soyuka

I created a repository for this purpose: https://github.com/xterr/api-platform-links-handler-trait

You can find a test case in the readme

Thank you

xterr avatar Dec 20 '23 12:12 xterr

This looks to be fixed by #6092

The tl;dr of what was happening was right here https://github.com/api-platform/core/blob/875cc155e556541c0591b0c182ed64dcc41b9984/src/GraphQl/Resolver/Factory/ResolverFactory.php#L41

$body was equal to 0 so the loose comparison sent things down the wrong code path.

GwendolenLynch avatar Apr 08 '24 13:04 GwendolenLynch