-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Fix deserializing nested arrays of objects with mixed keys #50933
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HypeMC
commented
Jul 11, 2023
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Outdated
Show resolved
Hide resolved
ed46804
to
3ff1be8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check the follow patch please?
diff --git a/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
index 05d8b79d1a..dfd023a437 100644
--- a/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
+++ b/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
@@ -49,9 +49,7 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz
$type = substr($type, 0, -2);
- $builtinTypes = array_map(static function (Type $keyType) {
- return $keyType->getBuiltinType();
- }, \is_array($keyType = $context['key_type'] ?? []) ? $keyType : [$keyType]);
+ $builtinTypes = array_map(static function ($keyType) { return $keyType->getBuiltinType(); }, (array) ($context['key_type'] ?? []));
foreach ($data as $key => $value) {
$subContext = $context;
@@ -109,20 +107,12 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz
*/
private function validateKeyType(array $builtinTypes, $key, string $path): void
{
if (!$builtinTypes) {
return;
}
- $unexpectedDataType = true;
foreach ($builtinTypes as $builtinType) {
if (('is_'.$builtinType)($key)) {
- $unexpectedDataType = false;
- break;
+ return;
}
}
- if ($unexpectedDataType) {
- throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, implode('", "', $builtinTypes), get_debug_type($key)), $key, $builtinTypes, $path, true);
- }
+ throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, implode('", "', $builtinTypes), get_debug_type($key)), $key, $builtinTypes, $path, true);
}
}
diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json
index 1b9537f8f8..11e67a8a7e 100644
--- a/src/Symfony/Component/Serializer/composer.json
+++ b/src/Symfony/Component/Serializer/composer.json
@@ -34,7 +34,7 @@
"symfony/http-kernel": "^4.4|^5.0|^6.0",
"symfony/mime": "^4.4|^5.0|^6.0",
"symfony/property-access": "^5.4|^6.0",
- "symfony/property-info": "^5.4.24|^6.0",
+ "symfony/property-info": "^5.4.24|^6.2.11",
"symfony/uid": "^5.3|^6.0",
"symfony/validator": "^4.4|^5.0|^6.0",
"symfony/var-dumper": "^4.4|^5.0|^6.0",
@@ -47,7 +47,7 @@
"phpdocumentor/type-resolver": "<1.4.0",
"symfony/dependency-injection": "<4.4",
"symfony/property-access": "<5.4",
- "symfony/property-info": "<5.4.24",
+ "symfony/property-info": "<5.4.24|>=6,<6.2.11",
"symfony/uid": "<5.3",
"symfony/yaml": "<4.4"
},
@nicolas-grekas The following part won't work: - $builtinTypes = array_map(static function (Type $keyType) {
- return $keyType->getBuiltinType();
- }, \is_array($keyType = $context['key_type'] ?? []) ? $keyType : [$keyType]);
+ $builtinTypes = array_map(static function ($keyType) { return $keyType->getBuiltinType(); }, (array) ($context['key_type'] ?? []));
|
3ff1be8
to
db0e893
Compare
Thank you @HypeMC. |
This was referenced Jul 29, 2023
Merged
Merged
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently an error is thrown when trying to deserialize the following nested array of objects:
The same happens when using generics, e.g.
ArrayCollection<Inner>
.