Closed
Description
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.