Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@benjilebon

Description

@benjilebon

Symfony version(s) affected

5.4+

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 & #57802

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