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: 9 additions & 0 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,22 @@ 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();

break;
}

if ($this->isCurrentLineComment()) {
continue;
}

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

Expand Down
67 changes: 67 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,73 @@ public function testParseMultiLineUnquotedString()
$this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
}

/**
* @dataProvider getUnquotedMultilineScalarIgnoresCommentsData
*/
public function testUnquotedMultilineScalarIgnoresComments(string $yaml, array $expected)
{
$this->assertSame($expected, $this->parser->parse($yaml));
}

public static function getUnquotedMultilineScalarIgnoresCommentsData()
{
yield 'comments interspersed' => [
<<<YAML
key: unquoted
# this comment should be ignored
next line
# another comment
final line
another_key: works
YAML,
[
'key' => 'unquoted next line final line',
'another_key' => 'works',
],
];

yield 'only comments' => [
<<<YAML
key: unquoted
# this comment should be ignored
# another comment
another_key: works
YAML,
[
'key' => 'unquoted',
'another_key' => 'works',
],
];

yield 'blank lines and comments' => [
<<<YAML
key: unquoted
next line

# this comment should be ignored
final line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is a valid example, all online parser that I found terminate parsing the value as soon as they stumble upon a comment line (see https://yaml-online-parser.appspot.com/?yaml=key%3A+foo%0A+bar%0A+%23+m%0A+foobar%0Akey2%3A+baz&type=json for such an example).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see also yaml/yaml#49

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @xabbuh, thank you for your feedback on the spec compliance (3.2.3.3).

I've opened a new PR to address this properly: #62409 which implements what I believe is the correct, spec-compliant behavior.

What do you think of this new approach? Is this the right way to go, or do you think we should prefer to keep a more permissive (doc-aligned) behavior?

Comments are ignored by the YAML parser and do not need to be indented according to the current level of nesting in a collection.

another_key: works
YAML,
[
'key' => "unquoted next line\nfinal line",
'another_key' => 'works',
],
];

yield 'comment at end' => [
<<<YAML
key: unquoted
next line
# comment at end
another_key: works
YAML,
[
'key' => 'unquoted next line',
'another_key' => 'works',
],
];
}

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