8000 bug #53361 [Serializer] Take unnamed variadic parameters into account… · symfony/symfony@2b7706a · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b7706a

Browse files
bug #53361 [Serializer] Take unnamed variadic parameters into account when denormalizing (thijsBreker)
This PR was merged into the 5.4 branch. Discussion ---------- [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. | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #53354 | License | MIT Commits ------- cbecdfe [Serializer] Take unnamed variadic parameters into account when denormalizing
2 parents af9fa66 + cbecdfe commit 2b7706a
8000

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

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

+1-1
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))) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 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+
public function getFoo(): string
30+
{
31+
return $this->foo;
32+
}
33+
34+
public function getBar(): int
35+
{
36+
return $this->bar;
37+
}
38+
39+
/** @return Dummy[] */
40+
public function getBaz(): array
41+
{
42+
return $this->baz;
43+
}
44+
}

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

+20
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,25 @@ public static function getNormalizer()
247248
yield [new ObjectNormalizer(null, null, null, $extractor)];
248249
}
249250

251+
public function testVariadicConstructorDenormalization()
252+
{
253+
$data = [
254+
'foo' => 'woo',
255+
'baz' => [
256+
['foo' => null, 'bar' => null, 'baz' => null, 'qux' => null],
257+
['foo' => null, 'bar' => null, 'baz' => null, 'qux' => null],
258+
],
259+
];
260+
261+
$normalizer = new ObjectNormalizer();
262+
$normalizer->setSerializer(new Serializer([$normalizer]));
263+
264+
$expected = new DummyWithWithVariadicParameterConstructor('woo', 1, new Dummy(), new Dummy());
265+
$actual = $normalizer->denormalize($data, DummyWithWithVariadicParameterConstructor::class);
266+
267+
$this->assertEquals($expected, $actual);
268+
}
269+
250270
public static function getNormalizerWithCustomNameConverter()
251271
{
252272
$extractor = new PhpDocExtractor();

0 commit comments

Comments
 (0)
0