Skip to content

[Yaml] Fixed YAML Parser does not ignore duplicate keys, violating YAML spec. #10902

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 16, 2014
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
24 changes: 21 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,37 @@ private static function parseMapping($mapping, &$i = 0)
switch ($mapping[$i]) {
case '[':
// nested sequence
$output[$key] = self::parseSequence($mapping, $i);
$value = self::parseSequence($mapping, $i);
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($output[$key])) {
$output[$key] = $value;
}
$done = true;
break;
case '{':
// nested mapping
$output[$key] = self::parseMapping($mapping, $i);
$value = self::parseMapping($mapping, $i);
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($output[$key])) {
$output[$key] = $value;
}
$done = true;
break;
case ':':
case ' ':
break;
default:
$output[$key] = self::parseScalar($mapping, array(',', '}'), array('"', "'"), $i);
$value = self::parseScalar($mapping, array(',', '}'), array('"', "'"), $i);
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($output[$key])) {
$output[$key] = $value;
}
$done = true;
--$i;
}
Expand Down
23 changes: 20 additions & 3 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,35 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
// if next line is less indented or equal, then it means that the current value is null
if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
$data[$key] = null;
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($data[$key])) {
$data[$key] = null;
}
} else {
$c = $this->getRealCurrentLineNb() + 1;
$parser = new Parser($c);
$parser->refs =& $this->refs;
$data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
$value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($data[$key])) {
$data[$key] = $value;
}
}
} else {
if ($isInPlace) {
$data = $this->refs[$isInPlace];
} else {
$data[$key] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);;
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($data[$key])) {
$data[$key] = $value;
}
}
}
} else {
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,53 @@ public function testMappingInASequence()
);
}

/**
* > It is an error for two equal keys to appear in the same mapping node.
* > In such a case the YAML processor may continue, ignoring the second
* > `key: value` pair and issuing an appropriate warning. This strategy
* > preserves a consistent information model for one-pass and random access
* > applications.
*
* @see http://yaml.org/spec/1.2/spec.html#id2759572
* @see http://yaml.org/spec/1.1/#id932806
*
* @covers \Symfony\Component\Yaml\Parser::parse
*/
public function testMappingDuplicateKeyBlock()
{
$input = <<<EOD
parent:
child: first
child: duplicate
parent:
child: duplicate
child: duplicate
EOD;
$expected = array(
'parent' => array(
'child' => 'first',
),
);
$this->assertSame($expected, Yaml::parse($input));
}

/**
* @covers \Symfony\Component\Yaml\Inline::parseMapping
*/
public function testMappingDuplicateKeyFlow()
{
$input = <<<EOD
parent: { child: first, child: duplicate }
parent: { child: duplicate, child: duplicate }
EOD;
$expected = array(
'parent' => array(
'child' => 'first',
),
);
$this->assertSame($expected, Yaml::parse($input));
}

public function testEmptyValue()
{
$input = <<<EOF
Expand Down