8000 Denormalizer tries to process null attributes even though they are nullable · Issue #27384 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Denormalizer tries to process null attributes even though they are nullable #27384
Closed
@fpalluel

Description

@fpalluel

Symfony version(s) affected: 4

Description
Symfony tries to denormalize nested models null attributes even though they are nullable. It occurs when those models use constructor methods.

How to reproduce

class Model
{
    /**
     * @var SubModel|null
     */
    private $subModel;

    public function __construct(?SubModel $subModel)
    {
        $this->subModel = $subModel;
    }
}

class SubModel
{
    /**
     * @var DateTimeImmutable|null
     */
    private $date;

    public function __construct(?DateTimeImmutable $date)
    {
        $this->date = $date;

    }
}

When trying to deserialize a Model (with a SubModel containing a null $date), a NotNormalizableValueException is thrown, with the following message : The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.

Possible Solution
As a workaround, use setters instead of constructors.

The problem seems to lie in AbstractNormalizer, in instantiateObject method : it tries to denormalize the constructor parameter, even if its value is null.

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

      0