8000 bug #14745 [Serializer] AbstractNormalizer::instantiateObject allow d… · symfony/symfony@2aeaa22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2aeaa22

Browse files
committed
bug #14745 [Serializer] AbstractNormalizer::instantiateObject allow default values when not optional (boekkooi)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] AbstractNormalizer::instantiateObject allow default values when not optional | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR fixes a bug in the AbstractNormalizer when denormalizing a array with a missing value but a default value set in the class constructor. Commits ------- 266d53e [Serializer] AbstractNormalizer::instantiateObject allow default values when not optional
2 parents c563bbc + 266d53e commit 2aeaa22

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ protected function instantiateObject(array $data, $class, array &$context, \Refl
328328
$params[] = $data[$key];
329329
// don't run set for a parameter passed to the constructor
330330
unset($data[$key]);
331-
} elseif ($constructorParameter->isOptional()) {
331+
} elseif ($constructorParameter->isDefaultValueAvailable()) {
332332
$params[] = $constructorParameter->getDefaultValue();
333333
} else {
334334
throw new RuntimeException(

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ public function testConstructorDenormalizeWithMissingOptionalArgument()
213213
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
214214
}
215215

216+
public function testConstructorDenormalizeWithOptionalDefaultArgument()
217+
{
218+
$obj = $this->normalizer->denormalize(
219+
array('bar' => 'test'),
220+
__NAMESPACE__.'\GetConstructorArgsWithDefaultValueDummy', 'any');
221+
$this->assertEquals(array(), $obj->getFoo());
222+
$this->assertEquals('test', $obj->getBar());
223+
}
224+
216225
public function testConstructorWithObjectDenormalize()
217226
{
218227
$data = new \stdClass();
@@ -660,6 +669,33 @@ public function otherMethod()
660669
}
661670
}
662671

672+
class GetConstructorArgsWithDefaultValueDummy
673+
{
674+
protected $foo;
675+
protected $bar;
676+
677+
public function __construct($foo = array(), $bar)
678+
{
679+
$this->foo = $foo;
680+
$this->bar = $bar;
681+
}
682+
683+
public function getFoo()
684+
{
685+
return $this->foo;
686+
}
687+
688+
public function getBar()
689+
{
690+
return $this->bar;
691+
}
692+
693+
public function otherMethod()
694+
{
695+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
696+
}
697+
}
698+
663699
class GetCamelizedDummy
664700
{
665701
private $kevinDunglas;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ public function testConstructorDenormalizeWithMissingOptionalArgument()
157157
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
158158
}
159159

160+
public function testConstructorDenormalizeWithOptionalDefaultArgument()
161+
{
162+
$obj = $this->normalizer->denormalize(
163+
array('bar' => 'test'),
164+
__NAMESPACE__.'\ObjectConstructorArgsWithDefaultValueDummy', 'any');
165+
$this->assertEquals(array(), $obj->getFoo());
166+
$this->assertEquals('test', $obj->getBar());
167+
}
168+
160169
public function testConstructorWithObjectDenormalize()
161170
{
162171
$data = new \stdClass();
@@ -566,3 +575,30 @@ public function otherMethod()
566575
throw new \RuntimeException('Dummy::otherMethod() should not be called');
567576
}
568577
}
578+
579+
class ObjectConstructorArgsWithDefaultValueDummy
580+
{
581+
protected $foo;
582+
protected $bar;
583+
584+
public function __construct($foo = array(), $bar)
585+
{
586+
$this->foo = $foo;
587+
$this->bar = $bar;
588+
}
589+
590+
public function getFoo()
591+
{
592+
return $this->foo;
593+
}
594+
595+
public function getBar()
596+
{
597+
return $this->bar;
598+
}
599+
600+
public function otherMethod()
601+
{
602+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
603+
}
604+
}

0 commit comments

Comments
 (0)
0