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

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
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 <[email protected]>
*
* 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