Skip to content

Commit

Permalink
[Serializer] Fix Normalizer not utilizing converted name for index va…
Browse files Browse the repository at this point in the history
…riadic param
  • Loading branch information
DidierLmn authored and nicolas-grekas committed Jul 7, 2023
1 parent e528ace commit 0a88ada
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,12 @@ protected function instantiateObject(array &$data, string $class, array &$contex
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
if ($constructorParameter->isVariadic()) {
if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
if (!\is_array($data[$paramName])) {
if (!\is_array($data[$key])) {
throw new RuntimeException(sprintf('Cannot create an instance of "%s" from serialized data because the variadic parameter "%s" can only accept an array.', $class, $constructorParameter->name));
}

$variadicParameters = [];
foreach ($data[$paramName] as $parameterKey => $parameterData) {
foreach ($data[$key] as $parameterKey => $parameterData) {
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
}

Expand Down
21 changes: 21 additions & 0 deletions Tests/Normalizer/AbstractNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
Expand Down Expand Up @@ -168,6 +169,7 @@ public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInput

/**
* @dataProvider getNormalizer
* @dataProvider getNormalizerWithCustomNameConverter
*/
public function testObjectWithVariadicConstructorTypedArguments(AbstractNormalizer $normalizer)
{
Expand Down Expand Up @@ -243,6 +245,25 @@ public static function getNormalizer()
yield [new ObjectNormalizer(null, null, null, $extractor)];
}

public static function getNormalizerWithCustomNameConverter()
{
$extractor = new PhpDocExtractor();
$nameConverter = new class() implements NameConverterInterface {
public function normalize(string $propertyName): string
{
return ucfirst($propertyName);
}

public function denormalize(string $propertyName): string
{
return lcfirst($propertyName);
}
};

yield [new PropertyNormalizer(null, $nameConverter, $extractor)];
yield [new ObjectNormalizer(null, $nameConverter, null, $extractor)];
}

public function testIgnore()
{
$classMetadata = new ClassMetadata(IgnoreDummy::class);
Expand Down

0 comments on commit 0a88ada

Please sign in to comment.