8000 [Serializer] AbstractObjectNormalizer does not consider pseudo type false · Issue #45151 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ThomasNunninger opened this issue Jan 24, 2022 · 1 comment

Comments

@ThomasNunninger
Copy link
Contributor

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

@ThomasNunninger
Copy link
Contributor Author

Symfony 4.4 throws

1) Symfony\Component\Serializer\Tests\Normalizer\ObjectNormalizerTest::testDenormalizeFalsePseudoType
InvalidArgumentException: "false" is not a valid PHP type.

/home/thomas/Projects/symfony/src/Symfony/Component/PropertyInfo/Type.php:65
/home/thomas/Projects/symfony/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php:347
/home/thomas/Projects/symfony/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php:312
/home/thomas/Projects/symfony/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php:157
/home/thomas/Projects/symfony/src/Symfony/Component/PropertyInfo/PropertyInfoExtractor.php:109
/home/thomas/Projects/symfony/src/Symfony/Component/PropertyInfo/PropertyInfoExtractor.php:74
/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:529
/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:414
/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:377
/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Serializer.php:196
/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php:895

Therefore I did not provide a fix for Symfony 4.4.

Symfony 5.4. (withoug the fix) throws

1) Symfony\Component\Serializer\Tests\Normalizer\ObjectNormalizerTest::testDenormalizeFalsePseudoType
Error: Call to undefined function is_false()

/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:580
/home/thomas/Projects/symfony/src/Symfony/Co
8000
mponent/Serializer/Normalizer/AbstractObjectNormalizer.php:401
/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Serializer.php:238
/home/thomas/Projects/symfony/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php:789

ThomasNunninger added a commit to ThomasNunninger/symfony that referenced this issue Jan 24, 2022
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.
ThomasNunninger added a commit to ThomasNunninger/symfony that referenced this issue Jan 24, 2022
AbstractObjectNormalizer to respect false pseudo type declaration.
ThomasNunninger added a commit to ThomasNunninger/symfony that referenced this issue Jan 24, 2022
…ct `false` pseudo type declaration

Added a manual check if the type is `false`. Instead of calling (a not
existing) `is_false()`, now the data is validated directly.
ThomasNunninger added a commit to ThomasNunninger/symfony that referenced this issue Jan 24, 2022
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.
ThomasNunninger added a commit to ThomasNunninger/symfony that referenced this issue Jan 24, 2022
…ct `false` pseudo type declaration

Added a manual check if the type is `false`. Instead of calling (a not
existing) `is_false()`, now the data is validated directly.
ThomasNunninger added a commit to ThomasNunninger/symfony that referenced this issue Jan 24, 2022
Removed a void return type in a test method.
ThomasNunninger added a commit to ThomasNunninger/symfony that referenced this issue Jan 24, 2022
Prefixed a globally namespaced PHP constant with a backslash.
nicolas-grekas added a commit that referenced this issue Jan 26, 2022
…pseudo type false (Thomas Nunninger)

This PR was squashed before being merged into the 5.3 branch.

Discussion
----------

[Serializer] Fix AbstractObjectNormalizer not considering pseudo type false

| Q             | A
| ------------- | ---
| Branch?       | 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #45151
| License       | MIT
| Doc PR        |

AbstractObjectNormalizer does not consider pseudo type false (bug #45151)

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.

Commits
-------

27d9eff [Serializer] Fix AbstractObjectNormalizer not considering pseudo type false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0