Skip to content

Commit

Permalink
bug #24417 [Yaml] parse references on merge keys (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.7 branch.

Discussion
----------

[Yaml] parse references on merge keys

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23587
| License       | MIT
| Doc PR        |

Commits
-------

dab72ab [Yaml] parse references on merge keys
  • Loading branch information
fabpot committed Oct 5, 2017
2 parents 253cde2 + dab72ab commit cb14798
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -181,7 +181,7 @@ private function doParse($value, $exceptionOnInvalidType = false, $objectSupport
$key = (string) $key;
}

if ('<<' === $key) {
if ('<<' === $key && (!isset($values['value']) || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
$mergeNode = true;
$allowOverwrite = true;
if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
Expand Down Expand Up @@ -226,14 +226,14 @@ private function doParse($value, $exceptionOnInvalidType = false, $objectSupport
$data += $parsed; // array union
}
}
} elseif (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
} elseif ('<<' !== $key && isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
$isRef = $matches['ref'];
$values['value'] = $matches['value'];
}

if ($mergeNode) {
// Merge keys
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#') || '<<' === $key) {
// hash
// if next line is less indented or equal, then it means that the current value is null
if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
Expand All @@ -244,9 +244,13 @@ private function doParse($value, $exceptionOnInvalidType = false, $objectSupport
}
} else {
$value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
// 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 ('<<' === $key) {
$this->refs[$refMatches['ref']] = $value;
$data += $value;
} elseif ($allowOverwrite || !isset($data[$key])) {
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
$data[$key] = $value;
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -1179,6 +1179,34 @@ public function testParserCleansUpReferencesBetweenRuns()
YAML;
$this->parser->parse($yaml);
}

public function testParseReferencesOnMergeKeys()
{
$yaml = <<<YAML
mergekeyrefdef:
a: foo
<<: &quux
b: bar
c: baz
mergekeyderef:
d: quux
<<: *quux
YAML;
$expected = array(
'mergekeyrefdef' => array(
'a' => 'foo',
'b' => 'bar',
'c' => 'baz',
),
'mergekeyderef' => array(
'd' => 'quux',
'b' => 'bar',
'c' => 'baz',
),
);

$this->assertSame($expected, $this->parser->parse($yaml));
}
}

class B
Expand Down

0 comments on commit cb14798

Please sign in to comment.