Skip to content

Commit

Permalink
[Yaml] Throw on duplicate key even when value is NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
olsavmic authored and fabpot committed Jun 2, 2024
1 parent 02d5fce commit f9df19b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* duplicate mapping keys throw a `ParseException` even when such key has a NULL value

7.1
---

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private function doParse(string $value, int $flags): mixed
if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !isset($data[$key])) {
if ($allowOverwrite || !\array_key_exists($key, $data)) {
if (null !== $subTag) {
$data[$key] = new TaggedValue($subTag, '');
} else {
Expand All @@ -320,7 +320,7 @@ private function doParse(string $value, int $flags): mixed
}

$data += $value;
} elseif ($allowOverwrite || !isset($data[$key])) {
} elseif ($allowOverwrite || !\array_key_exists($key, $data)) {
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if (null !== $subTag) {
Expand All @@ -336,7 +336,7 @@ private function doParse(string $value, int $flags): mixed
$value = $this->parseValue(rtrim($values['value']), $flags, $context);
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !isset($data[$key])) {
if ($allowOverwrite || !\array_key_exists($key, $data)) {
$data[$key] = $value;
} else {
throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,14 @@ public static function getParseExceptionOnDuplicateData()
EOD;
$tests[] = [$yaml, 'child_sequence', 6];

$yaml = <<<EOD
parent:
child:
child2:
child:
EOD;
$tests[] = [$yaml, 'child', 4];

return $tests;
}

Expand Down

0 comments on commit f9df19b

Please sign in to comment.