Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3561be7

Browse files
committed
bug #24462 [Yaml] parse references on merge keys with objects (xabbuh)
This PR was merged into the 3.3 branch. Discussion ---------- [Yaml] parse references on merge keys with objects | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23587, #24417 | License | MIT | Doc PR | Applies necessary changes on the `3.3` branch to make #24417 work with the `PARSE_OBJECT_FOR_MAP` flag. Commits ------- ce10f35 parse references on merge keys with objects
2 parents 1dfa63f + ce10f35 commit 3561be7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ private function doParse($value, $flags)
332332
$value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags);
333333
if ('<<' === $key) {
334334
$this->refs[$refMatches['ref']] = $value;
335+
336+
if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $value instanceof \stdClass) {
337+
$value = (array) $value;
338+
}
339+
335340
$data += $value;
336341
} elseif ($allowOverwrite || !isset($data[$key])) {
337342
// Spec: Keys MUST be unique; first one wins.

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,34 @@ public function testParseReferencesOnMergeKeys()
19411941

19421942
$this->assertSame($expected, $this->parser->parse($yaml));
19431943
}
1944+
1945+
public function testParseReferencesOnMergeKeysWithMappingsParsedAsObjects()
1946+
{
1947+
$yaml = <<<YAML
1948+
mergekeyrefdef:
1949+
a: foo
1950+
<<: &quux
1951+
b: bar
1952+
c: baz
1953+
mergekeyderef:
1954+
d: quux
1955+
<<: *quux
1956+
YAML;
1957+
$expected = (object) array(
1958+
'mergekeyrefdef' => (object) array(
1959+
'a' => 'foo',
1960+
'b' => 'bar',
1961+
'c' => 'baz',
1962+
),
1963+
'mergekeyderef' => (object) array(
1964+
'd' => 'quux',
1965+
'b' => 'bar',
1966+
'c' => 'baz',
1967+
),
1968+
);
1969+
1970+
$this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
1971+
}
19441972
}
19451973

19461974
class B

0 commit comments

Comments
 (0)