Skip to content

Commit 079faef

Browse files
committed
bug #37744 [Yaml] Fix for #36624; Allow PHP constant as first key in block (jnye)
This PR was squashed before being merged into the 3.4 branch. Discussion ---------- [Yaml] Fix for #36624; Allow PHP constant as first key in block | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #36624 | License | MIT | Doc PR | When using a PHP constant in the first key of a mapping the parser would throw an exception. However if you used a PHP constant in any other key but the first it was allowed. This fix allows PHP constant as the first key. I've included a test for this parser error along with the fix. Commits ------- 46f635c492 [Yaml] Fix for #36624; Allow PHP constant as first key in block
2 parents 36a366d + 12473fe commit 079faef

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

Parser.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,12 @@ private function doParse($value, $flags)
230230
$this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags)
231231
);
232232
} else {
233-
if (isset($values['leadspaces'])
234-
&& self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
233+
if (
234+
isset($values['leadspaces'])
235+
&& (
236+
'!' === $values['value'][0]
237+
|| self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
238+
)
235239
) {
236240
// this is a compact notation element, add to next block and parse
237241
$block = $values['value'];

Tests/ParserTest.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,34 @@ public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
20252025
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT | Yaml::PARSE_KEYS_AS_STRINGS));
20262026
}
20272027

2028+
public function testPhpConstantTagMappingAsScalarKey()
2029+
{
2030+
$yaml = <<<YAML
2031+
map1:
2032+
- foo: 'value_0'
2033+
!php/const 'Symfony\Component\Yaml\Tests\B::BAR': 'value_1'
2034+
map2:
2035+
- !php/const 'Symfony\Component\Yaml\Tests\B::FOO': 'value_0'
2036+
bar: 'value_1'
2037+
YAML;
2038+
$this->assertSame([
2039+
'map1' => [['foo' => 'value_0', 'bar' => 'value_1']],
2040+
'map2' => [['foo' => 'value_0', 'bar' => 'value_1']],
2041+
], $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
2042+
}
2043+
2044+
public function testTagMappingAsScalarKey()
2045+
{
2046+
$yaml = <<<YAML
2047+
map1:
2048+
- !!str 0: 'value_0'
2049+
!!str 1: 'value_1'
2050+
YAML;
2051+
$this->assertSame([
2052+
'map1' => [['0' => 'value_0', '1' => 'value_1']],
2053+
], $this->parser->parse($yaml));
2054+
}
2055+
20282056
public function testMergeKeysWhenMappingsAreParsedAsObjects()
20292057
{
20302058
$yaml = <<<YAML
@@ -2287,7 +2315,7 @@ public function testMultiLineComment()
22872315
parameters:
22882316
abc
22892317
2290-
# Comment
2318+
# Comment
22912319
YAML;
22922320

22932321
$this->assertSame(['parameters' => 'abc'], $this->parser->parse($yaml));

0 commit comments

Comments
 (0)