8000 [Serializer] AbstractNormalizer::instantiateObject allow default valu… · symfony/symfony@266d53e · GitHub
[go: up one dir, main page]

Skip to content

Commit 266d53e

Browse files
committed
[Serializer] AbstractNormalizer::instantiateObject allow default values when not optional
1 parent 58efb98 commit 266d53e

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
@@ -203,6 +203,15 @@ public function testConstructorDenormalizeWithMissingOptionalArgument()
203203
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
204204
}
205205

206+
public function testConstructorDenormalizeWithOptionalDefaultArgument()
207+
{
208+
$obj = $this->normalizer->denormalize(
209+
array('bar' => 'test'),
210+
__NAMESPACE__.'\GetConstructorArgsWithDefaultValueDummy', 'any');
211+
$this->assertEquals(array(), $obj->getFoo());
212+
$this->assertEquals('test', $obj->getBar());
213+
}
214+
206215
public function testConstructorWithObjectDenormalize()
207216
{
208217
$data = new \stdClass();
@@ -650,6 +659,33 @@ public function otherMethod()
650659
}
651660
}
652661

662+
class GetConstructorArgsWithDefaultValueDummy
663+
{
664+
protected $foo;
665+
protected $bar;
666+
667+
public function __construct($foo = array(), $bar)
668 8000 +
{
669+
$this->foo = $foo;
670+
$this->bar = $bar;
671+
}
672+
673+
public function getFoo()
674+
{
675+
return $this->foo;
676+
}
677+
678+
public function getBar()
679+
{
680+
return $this->bar;
681+
}
682+
683+
public function otherMethod()
684+
{
685+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
686+
}
687+
}
688+
653689
class GetCamelizedDummy
654690
{
655691
private $kevinDunglas;

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

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

150+
public function testConstructorDenormalizeWithOptionalDefaultArgument()
151+
{
152+
$obj = $this->normalizer->denormalize(
153+
array('bar' => 'test'),
154+
__NAMESPACE__.'\ObjectConstructorArgsWithDefaultValueDummy', 'any');
155+
$this->assertEquals(array(), $obj->getFoo());
156+
$this->assertEquals('test', $obj->getBar());
157+
}
158+
150159
public function testConstructorWithObjectDenormalize()
151160
{
152161
$data = new \stdClass();
@@ -556,3 +565,30 @@ public function otherMethod()
556565
throw new \RuntimeException('Dummy::otherMethod() should not be called');
557566
}
558567
}
568+
569+
class ObjectConstructorArgsWithDefaultValueDummy
570+
{
571+
protected $foo;
572+
protected $bar;
573+
574+
public function __construct($foo = array(), $bar)
575+
{
576+
$this->foo = $foo;
577+
$this->bar = $bar;
578+
}
579+
580+
public function getFoo()
581+
{
582+
return $this->foo;
583+
}
584+
585+
public function getBar()
586+
{
587+
return $this->bar;
588+
}
589+
590+
public function otherMethod()
591+
{
592+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
593+
}
594+
}

0 commit comments

Comments
 (0)
0