core icon indicating copy to clipboard operation
core copied to clipboard

Composite identifier does not support php 8.1 enum

Open wa12rior opened this issue 3 years ago • 1 comments

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.

wa12rior avatar Jun 30 '22 08:06 wa12rior

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)

soyuka avatar Jul 01 '22 15:07 soyuka

I think this is fair anwser. Closing.

wa12rior avatar Aug 18 '22 13:08 wa12rior