8000 Serializer thrown exception when property have "class-string<>" annotation · Issue #47736 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Serializer thrown exception when property have "class-string<>" annotation #47736

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
Tygygygydyk opened this issue Sep 30, 2022 · 5 comments
Closed

Comments

@Tygygygydyk
Copy link
Tygygygydyk commented Sep 30, 2022

Symfony version(s) affected

5.4

Description

When a class have a property annotated with class-string<> or string&class-string<> serializer throws an exception:

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

How to reproduce

<?php
require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

interface SomeInterface {
    public function get(): void;
}
class SomeClass implements SomeInterface {
    public function get(): void {}
}
class FooBar {
    /**
     * @var class-string<SomeInterface>
     */
    private $foo;

    /**
     * @param class-string<SomeInterface> $foo
     */
    public function __construct(string $foo) {
        $this->foo = $foo;
    }

    /**
     * @return class-string<SomeInterface>
     */
    public function getFoo(): string
    {
        return $this->foo;
    }
}

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$serializer = new Serializer([
    new DateTimeNormalizer(),
    new ArrayDenormalizer(),
    new ObjectNormalizer($classMetadataFactory, null, null, new PhpDocExtractor()),
], [new JsonEncoder()]);

$serializer->deserialize('{"foo": "'.SomeClass::class.'"}', FooBar::class, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);

Possible Solution

No response

Additional Context

No response

@derrabus
Copy link
Member

Does it work if you use PhpStanExtractor instead of PhpDocExtractor?

@astronom
Copy link
astronom commented Sep 30, 2022

Does it work if you use PhpStanExtractor instead of PhpDocExtractor?

It does not matter of extractor because both PhpDocTypeHelper and PhpStanTypeHelper from symfony/property-info did not recognize class-string as string and fallback to object

@xabbuh
Copy link
Member
xabbuh commented Sep 30, 2022

I think the reason for that is that support for the pseudo-types was introduced with #44451 in 6.1.

@astronom
Copy link

I think the reason for that is that support for the pseudo-types was introduced with #44451 in 6.1.

and seems there is no plans to backport it to 5.4 branch?

@derrabus
Copy link
Member

No, we don't do that. You'll have to upgrade or write your own extractor.

@derrabus derrabus closed this as not planned Won't fix, can't repro, duplicate, stale Sep 30, 2022
ambroisemaupate added a commit to roadiz/core-bundle-dev-app that referenced this issue Sep 29, 2023
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

5 participants
0