[Serializer] Allow null values when denormalizing with constructor missing data

This commit is contained in:
Daniel Gorgan 2018-11-04 02:29:33 +02:00 committed by Kévin Dunglas
parent 0615759e7c
commit 5fd0f3f453
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
2 changed files with 6 additions and 4 deletions

View File

@ -378,7 +378,7 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
// Don't run set for a parameter passed to the constructor
$params[] = $parameterData;
unset($data[$key]);
} elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) {
} elseif (array_key_exists($key, $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? array())) {
$params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
} elseif ($constructorParameter->isDefaultValueAvailable()) {
$params[] = $constructorParameter->getDefaultValue();

View File

@ -245,11 +245,11 @@ class ObjectNormalizerTest extends TestCase
$result = $normalizer->denormalize($data, DummyValueObject::class, 'json', array(
'default_constructor_arguments' => array(
DummyValueObject::class => array('foo' => '', 'bar' => ''),
DummyValueObject::class => array('foo' => '', 'bar' => '', 'baz' => null),
),
));
$this->assertEquals(new DummyValueObject(10, ''), $result);
$this->assertEquals(new DummyValueObject(10, '', null), $result);
}
public function testGroupsNormalize()
@ -1117,11 +1117,13 @@ class DummyValueObject
{
private $foo;
private $bar;
private $baz;
public function __construct($foo, $bar)
public function __construct($foo, $bar, $baz)
{
$this->foo = $foo;
$this->bar = $bar;
$this->baz = $baz;
}
}