Skip to content

Commit c725d27

Browse files
committed
Merge branch '2.3' into 2.4
* 2.3: 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 [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 Conflicts: src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
2 parents c5b52f0 + 9d5372a commit c725d27

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
@@ -131,7 +131,9 @@ public function denormalize($data, $class, $format = null, array $context = arra
131131
$params[] = $data[$paramName];
132132
// don't run set for a parameter passed to the constructor
133133
unset($data[$paramName]);
134-
} elseif (!$constructorParameter->isOptional()) {
134+
} elseif ($constructorParameter->isOptional()) {
135+
$params[] = $constructorParameter->getDefaultValue();
136+
} else {
135137
throw new RuntimeException(
136138
'Cannot create an instance of '.$class.
137139
' 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
@@ -105,6 +105,16 @@ public function testConstructorDenormalize()
105105
$this->assertEquals('bar', $obj->getBar());
106106
}
107107

108+
public function testConstructorDenormalizeWithMissingOptionalArgument()
109+
{
110+
$obj = $this->normalizer->denormalize(
111+
array('foo' => 'test', 'baz' => array(1, 2, 3)),
112+
__NAMESPACE__.'\GetConstructorOptionalArgsDummy', 'any');
113+
$this->assertEquals('test', $obj->getFoo());
114+
$this->assertEquals(array(), $obj->getBar());
115+
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
116+
}
117+
108118
/**
109119
* @dataProvider provideCallbacks
110120
*/
@@ -313,3 +323,37 @@ public function otherMethod()
313323
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
314324
{
315325
}
326+
327+
class GetConstructorOptionalArgsDummy
328+
{
329+
protected $foo;
330+
private $bar;
331+
private $baz;
332+
333+
public function __construct($foo, $bar = array(), $baz = array())
334+
{
335+
$this->foo = $foo;
336+
$this->bar = $bar;
337+
$this->baz = $baz;
338+
}
339+
340+
public function getFoo()
341+
{
342+
return $this->foo;
343+
}
344+
345+
public function getBar()
346+
{
347+
return $this->bar;
348+
}
349+
350+
public function getBaz()
351+
{
352+
return $this->baz;
353+
}
354+
355+
public function otherMethod()
356+
{
357+
throw new \RuntimeException("Dummy::otherMethod() should not be called");
358+
}
359+
}

0 commit comments

Comments
 (0)