Skip to content

Commit 5b05271

Browse files
bug symfony#50804 [Serializer] Fix Normalizer not utilizing converted name for index variadic param (DidierLmn)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Serializer] Fix Normalizer not utilizing converted name for index variadic param | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The AbstractNormalizer uses the wrong array index when setting variadic constructor parameters. It checks if the `$key` index (which is the normalized name) exists, but then tries to access the `$paramName` index (which is non-normalized name). This becomes an issue when using a custom NameConverter, for example one that switches between PascalCase and camelCase. Commits ------- 39c91e8 [Serializer] Fix Normalizer not utilizing converted name for index variadic param
2 parents 60d6ce1 + 39c91e8 commit 5b05271

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,12 @@ protected function instantiateObject(array &$data, string $class, array &$contex
358358
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
359359
if ($constructorParameter->isVariadic()) {
360360
if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
361-
if (!\is_array($data[$paramName])) {
361+
if (!\is_array($data[$key])) {
362362
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));
363363
}
364364

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2121
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2222
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
23+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2324
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2425
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2526
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
@@ -168,6 +169,7 @@ public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInput
168169

169170
/**
170171
* @dataProvider getNormalizer
172+
* @dataProvider getNormalizerWithCustomNameConverter
171173
*/
172174
public function testObjectWithVariadicConstructorTypedArguments(AbstractNormalizer $normalizer)
173175
{
@@ -243,6 +245,25 @@ public static function getNormalizer()
243245
yield [new ObjectNormalizer(null, null, null, $extractor)];
244246
}
245247

248+
public static function getNormalizerWithCustomNameConverter()
249+
{
250+
$extractor = new PhpDocExtractor();
251+
$nameConverter = new class() implements NameConverterInterface {
252+
public function normalize(string $propertyName): string
253+
{
254+
return ucfirst($propertyName);
255+
}
256+
257+
public function denormalize(string $propertyName): string
258+
{
259+
return lcfirst($propertyName);
260+
}
261+
};
262+
263+
yield [new PropertyNormalizer(null, $nameConverter, $extractor)];
264+
yield [new ObjectNormalizer(null, $nameConverter, null, $extractor)];
265+
}
266+
246267
public function testIgnore()
247268
{
248269
$classMetadata = new ClassMetadata(IgnoreDummy::class);

0 commit comments

Comments
 (0)