Skip to content

Commit 311e8f8

Browse files
committed
Merge branch '2.4' into 2.5
* 2.4: Fixed failed config schema loads due to libxml_disable_entity_loader usage. enabled PHP 5.6 for tests [Process] Fix ExecutableFinder with open basedir Refactored the CssSelector to remove the circular object graph Fix mocks to support >=5.5.14 and >=5.4.30 [ClassLoader] fixed PHP warning on PHP 5.3 [Validator] added Lithuanian translation for empty file Added missing dutch translations [Components][Serializer] optional constructor arguments can be omitted during the denormalization process [DomCrawler] properly handle buttons with single and double quotes inside the name attribute Added missing pt and pt_BR translations
2 parents b0505b6 + c725d27 commit 311e8f8

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Normalizer/GetSetMethodNormalizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ public function denormalize($data, $class, $format = null, array $context = arra
143143
$params[] = $data[$paramName];
144144
// don't run set for a parameter passed to the constructor
145145
unset($data[$paramName]);
146-
} elseif (!$constructorParameter->isOptional()) {
146+
} elseif ($constructorParameter->isOptional()) {
147+
$params[] = $constructorParameter->getDefaultValue();
148+
} else {
147149
throw new RuntimeException(
148150
'Cannot create an instance of '.$class.
149151
' from serialized data because its constructor requires '.

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ public function testConstructorDenormalize()
109109
$this->assertTrue($obj->isBaz());
110110
}
111111

112+
public function testConstructorDenormalizeWithMissingOptionalArgument()
113+
{
114+
$obj = $this->normalizer->denormalize(
115+
array('foo' => 'test', 'baz' => array(1, 2, 3)),
116+
__NAMESPACE__.'\GetConstructorOptionalArgsDummy', 'any');
117+
$this->assertEquals('test', $obj->getFoo());
118+
$this->assertEquals(array(), $obj->getBar());
119+
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
120+
}
121+
112122
/**
113123
* @dataProvider provideCallbacks
114124
*/
@@ -336,3 +346,37 @@ public function otherMethod()
336346
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
337347
{
338348
}
349+
350+
class GetConstructorOptionalArgsDummy
351+
{
352+
protected $foo;
353+
private $bar;
354+
private $baz;
355+
356+
public function __construct($foo, $bar = array(), $baz = array())
357+
{
358+
$this->foo = $foo;
359+
$this->bar = $bar;
360+
$this->baz = $baz;
361+
}
362+
363+
public function getFoo()
364+
{
365+
return $this->foo;
366+
}
367+
368+
public function getBar()
369+
{
370+
return $this->bar;
371+
}
372+
373+
public function getBaz()
374+
{
375+
return $this->baz;
376+
}
377+
378+
public function otherMethod()
379+
{
380+
throw new \RuntimeException("Dummy::otherMethod() should not be called");
381+
}
382+
}

0 commit comments

Comments
 (0)