Skip to content

[Serializer] AbstractObjectNormalizer does not consider pseudo type false #45151

Closed
@ThomasNunninger

Description

@ThomasNunninger

Symfony version(s) affected

5.4

Description

When you have a PHP 8.0 object where an attribute uses the pseudo type false (in a union type) you can't denormalize an array to that object.

How to reproduce

    public function testDenormalizeFalsePseudoType(): void
    {
        if (version_compare(PHP_VERSION, '8.0.0') < 0) {
            $this->markTestSkipped('This feature requires PHP 8.0');
        }

        // given a serializer that extracts the attribute types of an object via ReflectionExtractor
        $propertyTypeExtractor = new PropertyInfoExtractor(
            [],
            [new ReflectionExtractor()],
            [],
            [],
            []
        );
        $objectNormalizer = new ObjectNormalizer(null, null, null, $propertyTypeExtractor);

        $serializer = new Serializer([$objectNormalizer]);

        // when denormalizing some data into an object where an attribute uses the false pseudo type
        /** @var Php80Dummy $object */
        $object = $serializer->denormalize(
            [
                'canBeFalseOrString' => false,
            ],
            Php80Dummy::class
        );

        // then the attribute that declared false was filled correctly
        $this->assertFalse($object->canBeFalseOrString);
    }

Possible Solution

Pull request follows

Additional Context

No response

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