Skip to content

Commit 9f11f46

Browse files
committed
bug #57261 [Yaml] Throw on duplicate key even when value is NULL (olsavmic)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Yaml] Throw on duplicate key even when value is NULL | Q | A | ------------- | --- | Branch? | 5.4 (since v3.0) | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #57262 | License | MIT Duplicate keys are not valid by definition in YAML. The current implementation contains a bug that allows a key to be defined multiple times when the value is not set. ```yaml services: Foo: Bar: Foo: ``` Extends symfony/yaml@8094454 to throw when a key is set twice in YAML without a value. It may be technically a breaking change (as it suddenly makes some yaml-like files invalid), even though I'd classify it as a bugfix (as by definition, such files were not valid). If we classify it as a bug, we should probably backport the fix to the oldest maintained version. Commits ------- f9df19b [Yaml] Throw on duplicate key even when value is NULL
2 parents 02d5fce + f9df19b commit 9f11f46

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* duplicate mapping keys throw a `ParseException` even when such key has a NULL value
8+
49
7.1
510
---
611

src/Symfony/Component/Yaml/Parser.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private function doParse(string $value, int $flags): mixed
299299
if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
300300
// Spec: Keys MUST be unique; first one wins.
301301
// But overwriting is allowed when a merge node is used in current block.
302-
if ($allowOverwrite || !isset($data[$key])) {
302+
if ($allowOverwrite || !\array_key_exists($key, $data)) {
303303
if (null !== $subTag) {
304304
$data[$key] = new TaggedValue($subTag, '');
305305
} else {
@@ -320,7 +320,7 @@ private function doParse(string $value, int $flags): mixed
320320
}
321321

322322
$data += $value;
323-
} elseif ($allowOverwrite || !isset($data[$key])) {
323+
} elseif ($allowOverwrite || !\array_key_exists($key, $data)) {
324324
// Spec: Keys MUST be unique; first one wins.
325325
// But overwriting is allowed when a merge node is used in current block.
326326
if (null !== $subTag) {
@@ -336,7 +336,7 @@ private function doParse(string $value, int $flags): mixed
336336
$value = $this->parseValue(rtrim($values['value']), $flags, $context);
337337
// Spec: Keys MUST be unique; first one wins.
338338
// But overwriting is allowed when a merge node is used in current block.
339-
if ($allowOverwrite || !isset($data[$key])) {
339+
if ($allowOverwrite || !\array_key_exists($key, $data)) {
340340
$data[$key] = $value;
341341
} else {
342342
throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);

src/Symfony/Component/Yaml/Tests/ParserTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,14 @@ public static function getParseExceptionOnDuplicateData()
10261026
EOD;
10271027
$tests[] = [$yaml, 'child_sequence', 6];
10281028

1029+
$yaml = <<<EOD
1030+
parent:
1031+
child:
1032+
child2:
1033+
child:
1034+
EOD;
1035+
$tests[] = [$yaml, 'child', 4];
1036+
10291037
return $tests;
10301038
}
10311039

0 commit comments

Comments
 (0)