Permalink
Browse files

bug #29076 [Serializer] Allow null values when denormalizing with con…

…structor missing data (danut007ro)

This PR was squashed before being merged into the 4.1 branch (closes #29076).

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

When using `default_constructor_arguments` to denormalize objects, `null` values for a parameter weren't processed, so instantiating was failing.

Commits
-------

5fd0f3f [Serializer] Allow null values when denormalizing with constructor missing data
  • Loading branch information...
dunglas committed Nov 8, 2018
2 parents 097963f + 5fd0f3f commit ca5b64d0e0fed0a0d78d2ff8009e08c35d33bdd3
@@ -378,7 +378,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
// 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();
@@ -245,11 +245,11 @@ public function testFillWithEmptyDataWhenMissingData()
$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;
}
}

0 comments on commit ca5b64d

Please sign in to comment.