Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,6 @@ private function parseValue(string $value, int $flags, string $context): mixed
$lines = [];

while ($this->moveToNextLine()) {
if ($this->isCurrentLineBlank()) {
$lines[] = '';
continue;
}

// unquoted strings end before the first unindented line
if (0 === $this->getCurrentLineIndentation()) {
$this->moveToPreviousLine();
Expand All @@ -778,6 +773,10 @@ private function parseValue(string $value, int $flags, string $context): mixed
break;
}

if ('mapping' === $context && str_contains($this->currentLine, ': ') && !$this->isCurrentLineComment()) {
throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
}

$lines[] = trim($this->currentLine);
}

Expand Down
71 changes: 71 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,77 @@ public function testUnquotedMultilineScalarThrowsOnOrphanedLineAfterComment()
$this->parser->parse($yaml);
}

public function testUnquotedMultilineScalarWithBlankLines()
{
$yaml = <<<YAML
foo:
line 1

line 2
YAML;
$this->assertSame(['foo' => "line 1\nline 2"], $this->parser->parse($yaml));
}

/**
* @dataProvider provideInvalidYamlFiles
*/
public function testLineNumberInException(int $expectedLine, string $yaml, string $message)
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionMessage(\sprintf('at line %d', $expectedLine));

$this->parser->parse($yaml);
}

public static function provideInvalidYamlFiles(): iterable
{
yield 'invalid_nested_under_scalar' => [
4,
<<<YAML
en:
NONAMESPACE: Include Entity without Namespace
Invalid: Foo
About: 'About us'
- Invalid
YAML,
'Unable to parse',
];

yield 'invalid_nested_under_scalar_with_trailing_newlines' => [
4,
<<<YAML
en:
NONAMESPACE: Include Entity without Namespace
Invalid: Foo
About: 'About us'
- Invalid


YAML,
'Unable to parse',
];

yield 'colon_in_unquoted_value' => [
2,
<<<YAML
foo: bar
baz: qux
YAML,
'A colon cannot be used in an unquoted mapping value',
];

yield 'colon_in_unquoted_value_multiline' => [
2,
<<<YAML
foo:
bar
baz: qux
YAML,
'Mapping values are not allowed in multi-line blocks',
];
}

/**
* @dataProvider unquotedStringWithTrailingComment
*/
Expand Down
Loading