Skip to content

Commit f7cf88c

Browse files
committed
Throw on duplicate key even when value is NULL
1 parent 02d5fce commit f7cf88c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 0 deletions
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)