From 11128c9c0be9ba6fe50771a50a7904ce2889ebf8 Mon Sep 17 00:00:00 2001 From: Oleksii Svitiashchuk Date: Thu, 14 Jan 2021 23:57:52 +0200 Subject: [PATCH] [Messenger] Fix merging PrototypedArrayNode associative values --- composer.json | 3 +- .../Config/Definition/PrototypedArrayNode.php | 11 ++-- .../Definition/PrototypedArrayNodeTest.php | 52 +++++++++++++++++++ src/Symfony/Component/Config/composer.json | 3 +- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 63108b1e132e9..7f98fbd213517 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,8 @@ "symfony/polyfill-mbstring": "~1.0", "symfony/polyfill-php72": "~1.5", "symfony/polyfill-php73": "^1.11", - "symfony/polyfill-php80": "^1.15" + "symfony/polyfill-php80": "^1.15", + "symfony/polyfill-php81": "^1.22" }, "replace": { "symfony/asset": "self.version", diff --git a/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php b/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php index 68bb270172eb6..72d3578be7a09 100644 --- a/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php +++ b/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php @@ -225,11 +225,11 @@ protected function normalizeValue($value) $value = $this->remapXml($value); - $isAssoc = array_keys($value) !== range(0, \count($value) - 1); + $isList = array_is_list($value); $normalized = []; foreach ($value as $k => $v) { if (null !== $this->keyAttribute && \is_array($v)) { - if (!isset($v[$this->keyAttribute]) && \is_int($k) && !$isAssoc) { + if (!isset($v[$this->keyAttribute]) && \is_int($k) && $isList) { $ex = new InvalidConfigurationException(sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath())); $ex->setPath($this->getPath()); @@ -271,7 +271,7 @@ protected function normalizeValue($value) } $prototype = $this->getPrototypeForChild($k); - if (null !== $this->keyAttribute || $isAssoc) { + if (null !== $this->keyAttribute || !$isList) { $normalized[$k] = $prototype->normalize($v); } else { $normalized[] = $prototype->normalize($v); @@ -304,9 +304,10 @@ protected function mergeValues($leftSide, $rightSide) return $rightSide; } + $isList = array_is_list($rightSide); foreach ($rightSide as $k => $v) { - // prototype, and key is irrelevant, append the element - if (null === $this->keyAttribute) { + // prototype, and key is irrelevant there are no named keys, append the element + if (null === $this->keyAttribute && $isList) { $leftSide[] = $v; continue; } diff --git a/src/Symfony/Component/Config/Tests/Definition/PrototypedArrayNodeTest.php b/src/Symfony/Component/Config/Tests/Definition/PrototypedArrayNodeTest.php index 7a58ead8da967..58d2408a71c90 100644 --- a/src/Symfony/Component/Config/Tests/Definition/PrototypedArrayNodeTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/PrototypedArrayNodeTest.php @@ -338,4 +338,56 @@ public function getDataForKeyRemovedLeftValueOnly() ], ]; } + + /** + * @dataProvider getPrototypedArrayNodeDataToMerge + */ + public function testPrototypedArrayNodeMerge($left, $right, $expected) + { + $node = new PrototypedArrayNode('options'); + $node->setNormalizeKeys(false); + $node->setPrototype(new VariableNode('value')); + $node->setDefaultValue([]); + + $result = $node->merge($left, $right); + + self::assertSame($result, $expected); + } + + public function getPrototypedArrayNodeDataToMerge() + { + return [ + // data to merged is a plain array + [ + ['foo', 'bar'], + ['foo', 'baz', 'qux'], + ['foo', 'bar', 'foo', 'baz', 'qux'], + ], + // data to be merged is an associative array + [ + ['option1' => true, 'option2' => 'foo'], + [ + 'option2' => 'bar', + 'option3' => 42, + 'option4' => [ + 'option41' => 'baz', + 'option42' => [ + 'option423' => 'qux', + ], + ], + ], + [ + 'option1' => true, + 'option2' => 'bar', + 'option3' => 42, + 'option4' => [ + 'option41' => 'baz', + 'option42' => [ + 'option423' => 'qux', + ], + ], + ], + ], + ]; + } } diff --git a/src/Symfony/Component/Config/composer.json b/src/Symfony/Component/Config/composer.json index d71373bb8151f..13e4cd409f3ac 100644 --- a/src/Symfony/Component/Config/composer.json +++ b/src/Symfony/Component/Config/composer.json @@ -18,7 +18,8 @@ "require": { "php": ">=7.1.3", "symfony/filesystem": "^3.4|^4.0|^5.0", - "symfony/polyfill-ctype": "~1.8" + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-php81": "^1.22" }, "require-dev": { "symfony/event-dispatcher": "^3.4|^4.0|^5.0", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy