Skip to content

Commit 412589d

Browse files
olsavmicfabpot
authored andcommitted
[Yaml] Throw on duplicate key even when value is NULL
1 parent fa34c77 commit 412589d

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.md

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

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);

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)