Skip to content
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

[PropertyInfo] ReflectionExtractor::getType incorrectly reports nullable typed array of objects as a non-nullable CollectionType #57707

Open
benjilebon opened this issue Jul 11, 2024 · 0 comments

Comments

@benjilebon
Copy link

benjilebon commented Jul 11, 2024

Symfony version(s) affected

7.1.2

Description

Using ReflectionExtractor::getType on a property typed with ?array and an adder typed with an object incorrectly returns the type as non nullable.

This issue can lead to problems using symfony/serializer during denormalization for example, causing it to fail since the denormalizer expect the fetched type from the ReflectionExtractor to be non-nullable even when given property is properly declared as such

How to reproduce

An example using a Video class with an array of Comment class on a $comments property (phpDoc is only indicative here for clarity purposes)

class Video
{
    public ?string $title = null;

    /**
     * @var Comment[]|null
     */
    public array|null $comments;

    private function setTitle(?string $title): void
    {
        $this->title = $title;
    }

    private function addComment(Comment $comment): void
    {
        $this->comments[] = $comment;
    }
}
class Comment
{
    public string $content;
}
$extractor = new ReflectionExtractor();

$property = $extractor->getType(Video::class, 'comments');

$property->isNullable();
// returns false

Practical example with Xdebug :

image

Possible Solution

#57708

Additional Context

No response

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

2 participants