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

Appearance settings

[Messenger] Fix merging PrototypedArrayNode associative values #39847

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
merged 1 commit into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 3 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions 11 src/Symfony/Component/Config/Definition/PrototypedArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
],
];
}
}
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Config/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.