Skip to content

Commit de53bd6

Browse files
committed
bug #31031 [Serializer] MetadataAwareNameConverter: Do not assume that property names are strings (soyuka)
This PR was merged into the 4.2 branch. Discussion ---------- [Serializer] MetadataAwareNameConverter: Do not assume that property names are strings | Q | A | ------------- | --- | Branch? | 4.2 (class introduced in v4.2.3) | Bug fix? | yes | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes | Fixed tickets | api-platform/core#2709 | License | MIT | Doc PR | n/a When this class was introduced, there was an assumption made about the type of `propertyNames` and therefore a `: ?string` return type was introduced in the fallbacks/normalization private methods. Because symfony doesn't use strict mode yet (compatibility issues with php IIRC), when using a non-string property name (for example the integer `0` which is a valid property name in an array), it will convert the integer to a string. This is not good, especially if you have a name converter that returns the given property name (ie no transformation) you'll have it's type changed which isn't correct. I've discovered this bug while working on adding this name converter in api platform (api-platform/core#2709). Commits ------- af1e136 MetadataAwareNameConverter: Do not assume that property names are strings
2 parents 5859749 + af1e136 commit de53bd6

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function denormalize($propertyName, string $class = null, string $format
6969
return self::$denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
7070
}
7171

72-
private function getCacheValueForNormalization(string $propertyName, string $class): ?string
72+
private function getCacheValueForNormalization($propertyName, string $class)
7373
{
7474
if (!$this->metadataFactory->hasMetadataFor($class)) {
7575
return null;
@@ -83,12 +83,12 @@ private function getCacheValueForNormalization(string $propertyName, string $cla
8383
return $attributesMetadata[$propertyName]->getSerializedName() ?? null;
8484
}
8585

86-
private function normalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = []): string
86+
private function normalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
8787
{
8888
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
8989
}
9090

91-
private function getCacheValueForDenormalization(string $propertyName, string $class): ?string
91+
private function getCacheValueForDenormalization($propertyName, string $class)
9292
{
9393
if (!isset(self::$attributesMetadataCache[$class])) {
9494
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class);
@@ -97,7 +97,7 @@ private function getCacheValueForDenormalization(string $propertyName, string $c
9797
return self::$attributesMetadataCache[$class][$propertyName] ?? null;
9898
}
9999

100-
private function denormalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = []): string
100+
private function denormalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
101101
{
102102
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
103103
}

src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function attributeProvider()
102102
['foo', 'baz'],
103103
['bar', 'qux'],
104104
['quux', 'quux'],
105+
[0, 0],
105106
];
106107
}
107108

@@ -111,6 +112,7 @@ public function fallbackAttributeProvider()
111112
['foo', 'baz'],
112113
['bar', 'qux'],
113114
['quux', 'QUUX'],
115+
[0, 0],
114116
];
115117
}
116118
}

0 commit comments

Comments
 (0)