[Yaml] Fix for #36624; Allow PHP constant as first key in block

This commit is contained in:
Joshua Nye 2020-08-05 16:33:59 -04:00 committed by Fabien Potencier
parent 995d784a0d
commit 46f635c492
2 changed files with 35 additions and 3 deletions

View File

@ -230,8 +230,12 @@ class Parser
$this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags)
);
} else {
if (isset($values['leadspaces'])
&& self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
if (
isset($values['leadspaces'])
&& (
'!' === $values['value'][0]
|| self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
)
) {
// this is a compact notation element, add to next block and parse
$block = $values['value'];

View File

@ -2025,6 +2025,34 @@ YAML;
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT | Yaml::PARSE_KEYS_AS_STRINGS));
}
public function testPhpConstantTagMappingAsScalarKey()
{
$yaml = <<<YAML
map1:
- foo: 'value_0'
!php/const 'Symfony\Component\Yaml\Tests\B::BAR': 'value_1'
map2:
- !php/const 'Symfony\Component\Yaml\Tests\B::FOO': 'value_0'
bar: 'value_1'
YAML;
$this->assertSame([
'map1' => [['foo' => 'value_0', 'bar' => 'value_1']],
'map2' => [['foo' => 'value_0', 'bar' => 'value_1']],
], $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
}
public function testTagMappingAsScalarKey()
{
$yaml = <<<YAML
map1:
- !!str 0: 'value_0'
!!str 1: 'value_1'
YAML;
$this->assertSame([
'map1' => [['0' => 'value_0', '1' => 'value_1']],
], $this->parser->parse($yaml));
}
public function testMergeKeysWhenMappingsAreParsedAsObjects()
{
$yaml = <<<YAML
@ -2287,7 +2315,7 @@ YAML;
parameters:
abc
# Comment
# Comment
YAML;
$this->assertSame(['parameters' => 'abc'], $this->parser->parse($yaml));