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

Appearance settings

Commit c8c3c56

Browse filesBrowse files
ajgarlagnicolas-grekas
authored andcommitted
[Serializer] Fix denormalization of object with variadic constructor typed argument
1 parent b4364aa commit c8c3c56
Copy full SHA for c8c3c56

File tree

Expand file treeCollapse file tree

3 files changed

+55
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+55
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,13 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
354354
throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
355355
}
356356

357-
$params = array_merge($params, $data[$paramName]);
357+
$variadicParameters = [];
358+
foreach ($data[$paramName] as $parameterData) {
359+
$variadicParameters[] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
360+
}
361+
362+
$params = array_merge($params, $variadicParameters);
363+
unset($data[$key]);
358364
}
359365
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
360366
$parameterData = $data[$key];
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class VariadicConstructorTypedArgsDummy
15+
{
16+
private $foo;
17+
18+
public function __construct(Dummy ...$foo)
19+
{
20+
$this->foo = $foo;
21+
}
22+
23+
public function getFoo()
24+
{
25+
return $this->foo;
26+
}
27+
}

‎src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
99
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1010
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
11+
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
12+
use Symfony\Component\Serializer\Serializer;
1113
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
14+
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
1215
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
1316
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
1417
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
1518
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
19+
use Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
1620

1721
/**
1822
* Provides a dummy Normalizer which extends the AbstractNormalizer.
@@ -128,4 +132,21 @@ public function testObjectWithNullableConstructorArgument()
128132

129133
$this->assertNull($dummy->getFoo());
130134
}
135+
136+
/**
137+
* @requires PHP 5.6
138+
*/
139+
public function testObjectWithVariadicConstructorTypedArguments()
140+
{
141+
$normalizer = new PropertyNormalizer();
142+
$normalizer->setSerializer(new Serializer([$normalizer]));
143+
$data = ['foo' => [['foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz', 'qux' => 'Qux'], ['foo' => 'FOO', 'bar' => 'BAR', 'baz' => 'BAZ', 'qux' => 'QUX']]];
144+
$dummy = $normalizer->denormalize($data, VariadicConstructorTypedArgsDummy::class);
145+
146+
$this->assertInstanceOf(VariadicConstructorTypedArgsDummy::class, $dummy);
147+
$this->assertCount(2, $dummy->getFoo());
148+
foreach ($dummy->getFoo() as $foo) {
149+
$this->assertInstanceOf(Dummy::class, $foo);
150+
}
151+
}
131152
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.