Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:5.4fromHypeMC:fix-union-in-array-keys
Jul 27, 2023

Conversation

@HypeMC
Copy link
Member

QA
Branch?5.4
Bug fix?yes
New feature?no
Deprecations?no
TicketsFix#50675
LicenseMIT
Doc PR-

Currently an error is thrown when trying to deserialize the following nested array of objects:

useSymfony\Component\PropertyInfo\Extractor\PhpDocExtractor;useSymfony\Component\Serializer\Encoder\JsonEncoder;useSymfony\Component\Serializer\Normalizer\ArrayDenormalizer;useSymfony\Component\Serializer\Normalizer\ObjectNormalizer;useSymfony\Component\Serializer\Serializer;$serializer =newSerializer([newObjectNormalizer(null,null,null,newPhpDocExtractor()),newArrayDenormalizer(),], ['json' =>newJsonEncoder()]);class Outer{/**     * @var array<int|string, Inner>     */publicarray$inners;}class Inner{publicstring$name;}$serializer->deserialize('{"inners": {"1": {"name": "One"}, "two": {"name": "Two"}}}', Outer::class,'json');
Fatal error: Uncaught Symfony\Component\Serializer\Exception\NotNormalizableValueException:The type of the key "two" must be "int" ("string" given).

The same happens when using generics, e.g.ArrayCollection<Inner>.

@HypeMCHypeMCforce-pushed thefix-union-in-array-keys branch 3 times, most recently fromed46804 to3ff1be8CompareJuly 13, 2023 16:44
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

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.phpindex 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.jsonindex 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"     },

@HypeMC
Copy link
MemberAuthor

Can you check the follow patch please?

@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'] ?? []));

$context['key_type'] is not a scalar but an instance of\Symfony\Component\PropertyInfo\Type.

@HypeMCHypeMCforce-pushed thefix-union-in-array-keys branch from3ff1be8 todb0e893CompareJuly 27, 2023 15:54
@nicolas-grekas
Copy link
Member

Thank you@HypeMC.

@nicolas-grekasnicolas-grekas merged commit8492f10 intosymfony:5.4Jul 27, 2023
@HypeMCHypeMC deleted the fix-union-in-array-keys branchJuly 27, 2023 16:19
This was referencedJul 29, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

5.4

Development

Successfully merging this pull request may close these issues.

3 participants

@HypeMC@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp