core
core copied to clipboard
Composite identifier does not support php 8.1 enum
API Platform version(s) affected: 2.6.8
Description
CompositeIdentifierParser does not handle php 8.1 enums. Enum object does not support toString magic method so it is impossible to make a workaround.
How to reproduce
Create an entity with fields below
/**
* @ORM\Id()
* @ORM\Column(type="integer")
* @Groups({"entity:view", "entity:list"})
*/
private int $id;
/**
* @ORM\Id()
* @ORM\Column(type="string", enumType=Code::class)
* @Groups({"entity:view", "entity:list"})
*/
private Code $code;
The error we get whenever we make GET request for this resource is:
Object of class Foo\Bar\Code could not be converted to string
"file" => "/app/vendor/api-platform/core/src/Identifier/CompositeIdentifierParser.php"
"line" => 58
Possible solution
Change enum type to string in the entity. But we lose enum benefits. Another solution might be to add an enum normalizer.
enums are not supported yet but we're planing to add this in API Platform 3 which lowest target is php 8.1 (we could not make this happen in 2.7)
I think this is fair anwser. Closing.