Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd29e433

Browse files
pounardnicolas-grekas
authored andcommitted
[Serializer] AbstractNormalizer force null for non-optional nullable constructor parameter denormalization when not present in input
1 parent602b520 commitd29e433

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
@@ -501,6 +501,8 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
501501
$params[] =$this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
502502
}elseif ($constructorParameter->isDefaultValueAvailable()) {
503503
$params[] =$constructorParameter->getDefaultValue();
504+
}elseif ($constructorParameter->hasType() &&$constructorParameter->getType()->allowsNull()) {
505+
$params[] =null;
504506
}else {
505507
thrownewMissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.',$class,$constructorParameter->name));
506508
}
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+
namespaceSymfony\Component\Serializer\Tests\Fixtures;
13+
14+
class NullableOptionalConstructorArgumentDummy
15+
{
16+
private$foo;
17+
18+
publicfunction__construct(?\stdClass$foo)
19+
{
20+
$this->foo =$foo;
21+
}
22+
23+
publicfunctionsetFoo($foo)
24+
{
25+
$this->foo ='this setter should not be called when using the constructor argument';
26+
}
27+
28+
publicfunctiongetFoo()
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
@@ -18,6 +18,7 @@
1818
useSymfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
1919
useSymfony\Component\Serializer\Tests\Fixtures\Dummy;
2020
useSymfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
21+
useSymfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
2122
useSymfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
2223
useSymfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
2324
useSymfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
@@ -118,13 +119,37 @@ public function testObjectWithStaticConstructor()
118119
}
119120

120121
publicfunctiontestObjectWithNullableConstructorArgument()
122+
{
123+
$normalizer =newObjectNormalizer();
124+
$dummy =$normalizer->denormalize(['foo' =>null], NullableOptionalConstructorArgumentDummy::class);
125+
126+
$this->assertNull($dummy->getFoo());
127+
}
128+
129+
publicfunctiontestObjectWithNullableConstructorArgumentWithoutInput()
130+
{
131+
$normalizer =newObjectNormalizer();
132+
$dummy =$normalizer->denormalize([], NullableOptionalConstructorArgumentDummy::class);
133+
134+
$this->assertNull($dummy->getFoo());
135+
}
136+
137+
publicfunctiontestObjectWithNullableNonOptionalConstructorArgument()
121138
{
122139
$normalizer =newObjectNormalizer();
123140
$dummy =$normalizer->denormalize(['foo' =>null], NullableConstructorArgumentDummy::class);
124141

125142
$this->assertNull($dummy->getFoo());
126143
}
127144

145+
publicfunctiontestObjectWithNullableNonOptionalConstructorArgumentWithoutInput()
146+
{
147+
$normalizer =newObjectNormalizer();
148+
$dummy =$normalizer->denormalize([], NullableConstructorArgumentDummy::class);
149+
150+
$this->assertNull($dummy->getFoo());
151+
}
152+
128153
/**
129154
* @dataProvider getNormalizer
130155
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp