Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed92235

Browse files
committed
[Serializer] Take unnamed variadic parameters into account when denormalizing
We shouldn't break when a constructor has variadic parameters without named keys in the array.
1 parent bf62cbe commit ed92235

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
372372
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
373373
}
374374

375-
$params = array_merge($params, $variadicParameters);
375+
$params = array_merge(array_values($params), $variadicParameters);
376376
$unsetKeys[] = $key;
377377
}
378378
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 DummyWithWithVariadicParameterConstructor
15+
{
16+
private $foo;
17+
18+
private $bar;
19+
20+
private $baz;
21+
22+
public function __construct(string $foo, int $bar = 1, Dummy ...$baz)
23+
{
24+
$this->foo = $foo;
25+
$this->bar = $bar;
26+
$this->baz = $baz;
27+
}
28+
29+
/** @return string */
30+
public function getFoo()
31+
{
32+
return $this->foo;
33+
}
34+
35+
/** @return int */
36+
public function getBar()
37+
{
38+
return $this->bar;
39+
}
40+
41+
/** @return Dummy[] */
42+
public function getBaz()
43+
{
44+
return $this->baz;
45+
}
46+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
3030
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy;
3131
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
32+
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithWithVariadicParameterConstructor;
3233
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
3334
use Symfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
3435
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
@@ -247,6 +248,19 @@ public static function getNormalizer()
247248
yield [new ObjectNormalizer(null, null, null, $extractor)];
248249
}
249250

251+
public function testVariadicConstructorDenormalization()
252+
{
253+
$data = '{"foo":"woo","baz":[{"foo":null,"bar":null,"baz":null,"qux":null},{"foo":null,"bar":null,"baz":null,"qux":null}]}';
254+
$expected = new DummyWithWithVariadicParameterConstructor('woo', 1, new Dummy(), new Dummy());
255+
256+
$normalizer = new ObjectNormalizer();
257+
$serializer = new Serializer([$normalizer], [new JsonEncoder()]);
258+
$normalizer->setSerializer($serializer);
259+
260+
$actual = $normalizer->denormalize(json_decode($data, true), DummyWithWithVariadicParameterConstructor::class);
261+
$this->assertEquals($expected, $actual);
262+
}
263+
250264
public static function getNormalizerWithCustomNameConverter()
251265
{
252266
$extractor = new PhpDocExtractor();

0 commit comments

Comments
 (0)