10000 [Serializer] Take unnamed variadic parameters into account when denormalizing by thijsBreker · Pull Request #53361 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Take unnamed variadic parameters into account when denormalizing #53361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Take unnamed variadic parameters into account when denor…
…malizing

We shouldn't break when a constructor has variadic parameters without
named keys in the array.
  • Loading branch information
thijsBreker committed Jan 8, 2024
commit cbecdfef0da7333b5c1a0a205c57a6aa83af0a27
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
}

$params = array_merge($params, $variadicParameters);
$params = array_merge(array_values($params), $variadicParameters);
$unsetKeys[] = $key;
}
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

class DummyWithWithVariadicParameterConstructor
{
private $foo;

private $bar;

private $baz;

public function __construct(string $foo, int $bar = 1, Dummy ...$baz)
{
$this->foo = $foo;
$this->bar = $bar;
$this->baz = $baz;
}

public function getFoo(): string
{
return $this->foo;
}

public function getBar(): int
{
return $this->bar;
}

/** @return Dummy[] */
public function getBaz(): array
{
return $this->baz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithWithVariadicParameterConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
Expand Down Expand Up @@ -247,6 +248,25 @@ public static function getNormalizer()
yield [new ObjectNormalizer(null, null, null, $extractor)];
}

public function testVariadicConstructorDenormalization()
{
$data = [
'foo' => 'woo',
'baz' => [
['foo' => null, 'bar' => null, 'baz' => null, 'qux' => null],
['foo' => null, 'bar' => null, 'baz' => null, 'qux' => null],
],
];

$normalizer = new ObjectNormalizer();
$normalizer->setSerializer(new Serializer([$normalizer]));

$expected = new DummyWithWithVariadicParameterConstructor('woo', 1, new Dummy(), new Dummy());
$actual = $normalizer->denormalize($data, DummyWithWithVariadicParameterConstructor::class);

$this->assertEquals($expected, $actual);
}

public static function getNormalizerWithCustomNameConverter()
{
$extractor = new PhpDocExtractor();
Expand Down
0