Skip to content

NotNormalizableValueException with value-of #62636

@VincentLanglet

Description

@VincentLanglet

Symfony version(s) affected

not sure ? At least 7.3+

Description

While bumping my dependencies to the latest one I'm getting

Symfony\Component\Serializer\Exception\NotNormalizableValueException: The type of the "foo" attribute for class "MyClass" must be one of "value-of<MyEnum>" ("string" given).

I'm getting this error with both 7.3 and 7.4

How to reproduce

This seems to occur with the following code

final class MyClass
{
    /**
     * @param value-of<MyEnum> $foo
     * @param value-of<AnotherEnum> $bar
     */
    public function __construct(
        public string $foo,
        public string $bar,
    ) {
    }

when I tried to denormalize data like ['foo' => 'valueFoo', 'bar' => 'valueBar'] into MyClass.

I'm not sure how the bump introduce the error since I don't think SF version changed. But I wonder if there is some uncontrolled changes like the fact validateAndDenormalizeLegacy might be call before and now it's validateAndDenormalize in the AbstractObjectNormalizer...

Possible Solution

No idea.

Additional Context

I wonder if it's related to #60655 cc @stof @mtarld

It was said that

Be careful, there is 2 different value-of supported cases in phpstan and psalm:

value-of<T> with T being a BackedEnum type returns the union of all backing values of that enum (which we can generalize to the backing type as done in this PR)
value-of<T> with T being an array type returns the type of values in that array (there is also key-of<T> for the equivalent for the key type)

But if you look at https://phpstan.org/r/87ffbd66-f32f-4559-abe0-8d6f0c134694, value-of is used for string param or int param, not Enum typehint.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions