bug #29076 [Serializer] Allow null values when denormalizing with constructor 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
-------

5fd0f3f453 [Serializer] Allow null values when denormalizing with constructor missing data
This commit is contained in:
Kévin Dunglas 2018-11-08 22:02:02 +01:00
commit ca5b64d0e0
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;
}
}