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

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-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: 44 additions & 0 deletions
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

Lines changed: 20 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,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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy