Skip to content

Commit 2dc3e97

Browse files
committed
issue #40511 - AbstractNormalizer force null for non-optional nullable constructor parameter denormalization when not present in input
1 parent 6c0786b commit 2dc3e97

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ protected function instantiateObject(array &$data, string $class, array &$contex
396396
$params[] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
397397
} elseif ($constructorParameter->isDefaultValueAvailable()) {
398398
$params[] = $constructorParameter->getDefaultValue();
399+
} elseif ($constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
400+
$params[] = null;
399401
} else {
400402
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name));
401403
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class NullableOptionalConstructorArgumentDummy
15+
{
16+
private $foo;
17+
18+
public function __construct(?\stdClass $foo = null)
19+
{
20+
$this->foo = $foo;
21+
}
22+
23+
public function setFoo($foo)
24+
{
25+
$this->foo = 'this setter should not be called when using the constructor argument';
26+
}
27+
28+
public function getFoo()
29+
{
30+
return $this->foo;
31+
}
32+
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy;
2020
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
2121
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
22+
use Symfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
2223
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
2324
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
2425
use Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
@@ -125,13 +126,37 @@ public function testObjectWithStaticConstructor()
125126
}
126127

127128
public function testObjectWithNullableConstructorArgument()
129+
{
130+
$normalizer = new ObjectNormalizer();
131+
$dummy = $normalizer->denormalize(['foo' => null], NullableOptionalConstructorArgumentDummy::class);
132+
133+
$this->assertNull($dummy->getFoo());
134+
}
135+
136+
public function testObjectWithNullableConstructorArgumentWithoutInput()
137+
{
138+
$normalizer = new ObjectNormalizer();
139+
$dummy = $normalizer->denormalize([], NullableOptionalConstructorArgumentDummy::class);
140+
141+
$this->assertNull($dummy->getFoo());
142+
}
143+
144+
public function testObjectWithNullableNonOptionalConstructorArgument()
128145
{
129146
$normalizer = new ObjectNormalizer();
130147
$dummy = $normalizer->denormalize(['foo' => null], NullableConstructorArgumentDummy::class);
131148

132149
$this->assertNull($dummy->getFoo());
133150
}
134151

152+
public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInput()
153+
{
154+
$normalizer = new ObjectNormalizer();
155+
$dummy = $normalizer->denormalize([], NullableConstructorArgumentDummy::class);
156+
157+
$this->assertNull($dummy->getFoo());
158+
}
159+
135160
/**
136161
* @dataProvider getNormalizer
137162
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy