From f2cf86250344f692cbdce55f88a0977d2a5c20e5 Mon Sep 17 00:00:00 2001 From: Younes ENNAJI Date: Wed, 12 Nov 2025 00:27:57 +0100 Subject: [PATCH] [Yaml] Fix parsing of unquoted multiline scalars with comments or blank lines --- src/Symfony/Component/Yaml/Parser.php | 9 +++ .../Component/Yaml/Tests/ParserTest.php | 67 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 40702ed639e7a..ebfd27d432404 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -762,6 +762,11 @@ 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(); @@ -769,6 +774,10 @@ private function parseValue(string $value, int $flags, string $context): mixed break; } + if ($this->isCurrentLineComment()) { + continue; + } + $lines[] = trim($this->currentLine); } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index ebf032795fcec..b523612a6ed93 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -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' => [ + << 'unquoted next line final line', + 'another_key' => 'works', + ], + ]; + + yield 'only comments' => [ + << 'unquoted', + 'another_key' => 'works', + ], + ]; + + yield 'blank lines and comments' => [ + << "unquoted next line\nfinal line", + 'another_key' => 'works', + ], + ]; + + yield 'comment at end' => [ + << 'unquoted next line', + 'another_key' => 'works', + ], + ]; + } + /** * @dataProvider unquotedStringWithTrailingComment */