Skip to content

Commit

Permalink
bug #29076 [Serializer] Allow null values when denormalizing with con…
Browse files Browse the repository at this point in the history
…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 ca5b64d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit ca5b64d

Please sign in to comment.