8000 bug #25043 [Yaml] added ability for substitute aliases when mapping i… · symfony/symfony@abe6e92 · GitHub
[go: up one dir, main page]

Skip to content

Commit abe6e92

Browse files
committed
bug #25043 [Yaml] added ability for substitute aliases when mapping is on single line (Michał Strzelecki, xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [Yaml] added ability for substitute aliases when mapping is on single line | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11569 | License | MIT | Doc PR | Commits ------- dd26c80 substitute aliases in inline mappings 675a3fe added ability for substitute aliases when mapping in YAML is on single line
2 parents a809ab2 + dd26c80 commit abe6e92

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
365365
$output = array();
366366
$len = strlen($mapping);
367367
++$i;
368+
$allowOverwrite = false;
368369

369370
// {foo: bar, bar:foo, ...}
370371
while ($i < $len) {
@@ -384,6 +385,10 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
384385
// key
385386
$key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false);
386387

388+
if ('<<' === $key) {
389+
$allowOverwrite = true;
390+
}
391+
387392
// value
388393
$done = false;
389394

@@ -395,7 +400,12 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
395400
// Spec: Keys MUST be unique; first one wins.
396401
// Parser cannot abort this mapping earlier, since lines
397402
// are processed sequentially.
398-
if (!isset($output[$key])) {
403+
// But overwriting is allowed when a merge node is used in current block.
404+
if ('<<' === $key) {
405+
foreach ($value as $parsedValue) {
406+
$output += $parsedValue;
407+
}
408+
} elseif ($allowOverwrite || !isset($output[$key])) {
399409
$output[$key] = $value;
400410
}
401411
$done = true;
@@ -406,7 +416,10 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
406416
// Spec: Keys MUST be unique; first one wins.
407417
// Parser cannot abort this mapping earlier, since lines
408418
// are processed sequentially.
409-
if (!isset($output[$key])) {
419+
// But overwriting is allowed when a merge node is used in current block.
420+
if ('<<' === $key) {
421+
$output += $value;
422+
} elseif ($allowOverwrite || !isset($output[$key])) {
410423
$output[$key] = $value;
411424
}
412425
$done = true;
@@ -419,7 +432,10 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
419432
// Spec: Keys MUST be unique; first one wins.
420433
// Parser cannot abort this mapping earlier, since lines
421434
// are processed sequentially.
422-
if (!isset($output[$key])) {
435+
// But overwriting is allowed when a merge node is used in current block.
436+
if ('<<' === $key) {
437+
$output += $value;
438+
} elseif ($allowOverwrite || !isset($output[$key])) {
423439
$output[$key] = $value;
424440
}
425441
$done = true;

src/Symfony/Component/Yaml/Tests/Fixtures/sfMergeKey.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ yaml: |
2222
foo: bar
2323
foo: ignore
2424
bar: foo
25+
bar_inline: {a: before, d: other, <<: *foo, b: new, x: Oren, c: { foo: bar, foo: ignore, bar: foo}}
2526
duplicate:
2627
foo: bar
2728
foo: ignore
@@ -46,15 +47,20 @@ yaml: |
4647
p: 12345
4748
z:
4849
<<: *nestedref
50+
head_inline: &head_inline { <<: [ *foo , *dong , *foo2 ] }
51+
recursive_inline: { <<: *head_inline, c: { <<: *foo2 } }
4952
php: |
5053
array(
5154
'foo' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull'),
5255
'bar' => array('a' => 'before', 'd' => 'other', 'e' => null, 'b' => 'new', 'c' => array('foo' => 'bar', 'bar' => 'foo'), 'x' => 'Oren'),
56+
'bar_inline' => array('a' => 'before', 'd' => 'other', 'b' => 'new', 'c' => array('foo' => 'bar', 'bar' => 'foo'), 'e' => 'notnull', 'x' => 'Oren'),
5357
'duplicate' => array('foo' => 'bar'),
5458
'foo2' => array('a' => 'Ballmer'),
5559
'ding' => array('fi', 'fei', 'fo', 'fam'),
5660
'check' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam', 'isit' => 'tested'),
5761
'head' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam'),
5862
'taz' => array('a' => 'Steve', 'w' => array('p' => 1234)),
59-
'nested' => array('a' => 'Steve', 'w' => array('p' => 12345), 'd' => 'Doug', 'z' => array('p' => 12345))
63+
'nested' => array('a' => 'Steve', 'w' => array('p' => 12345), 'd' => 'Doug', 'z' => array('p' => 12345)),
64+
'head_inline' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam'),
65+
'recursive_inline' => array('a' => 'Steve', 'b' => 'Clark', 'c' => array('a' => 'Ballmer'), 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam'),
6066
)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,18 @@ public function testParseReferencesOnMergeKeys()
12071207

12081208
$this->assertSame($expected, $this->parser->parse($yaml));
12091209
}
1210+
1211+
/**
1212+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1213+
* @expectedExceptionMessage Reference "foo" does not exist
1214+
*/
1215+
public function testEvalRefException()
1216+
{
1217+
$yaml = <<<EOE
1218+
foo: { &foo { a: Steve, <<: *foo} }
1219+
EOE;
1220+
$this->parser->parse($yaml);
1221+
}
12101222
}
12111223

12121224
class B

0 commit comments

Comments
 (0)
0