Skip to content

Commit af78d51

Browse files
bug #62359 [Yaml] Fix parsing of unquoted multiline scalars with comments or blank lines (yoeunes)
This PR was merged into the 6.4 branch. Discussion ---------- [Yaml] Fix parsing of unquoted multiline scalars with comments or blank lines | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT This PR fixes two bugs when parsing unquoted multiline scalars: 1. **Indented comment lines** were incorrectly treated as empty strings, adding unwanted spaces to the final value. 2. **Indented blank lines** were incorrectly detected as having 0 indentation, causing the parser to stop early and throw a `ParseException`. For example, the following YAML would previously fail or parse incorrectly: ```yaml key: unquoted scalar next line # this comment added an unwanted space (Bug 1) final line # The blank line above caused a ParseException (Bug 2) another_key: value Commits ------- f2cf862 [Yaml] Fix parsing of unquoted multiline scalars with comments or blank lines
2 parents e4c32e0 + f2cf862 commit af78d51

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,22 @@ private function parseValue(string $value, int $flags, string $context): mixed
762762
$lines = [];
763763

764764
while ($this->moveToNextLine()) {
765+
if ($this->isCurrentLineBlank()) {
766+
$lines[] = '';
767+
continue;
768+
}
769+
765770
// unquoted strings end before the first unindented line
766771
if (0 === $this->getCurrentLineIndentation()) {
767772
$this->moveToPreviousLine();
768773

769774
break;
770775
}
771776

777+
if ($this->isCurrentLineComment()) {
778+
continue;
779+
}
780+
772781
$lines[] = trim($this->currentLine);
773782
}
774783

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,73 @@ public function testParseMultiLineUnquotedString()
17511751
$this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
17521752
}
17531753

1754+
/**
1755+
* @dataProvider getUnquotedMultilineScalarIgnoresCommentsData
1756+
*/
1757+
public function testUnquotedMultilineScalarIgnoresComments(string $yaml, array $expected)
1758+
{
1759+
$this->assertSame($expected, $this->parser->parse($yaml));
1760+
}
1761+
1762+
public static function getUnquotedMultilineScalarIgnoresCommentsData()
1763+
{
1764+
yield 'comments interspersed' => [
1765+
<<<YAML
1766+
key: unquoted
1767+
# this comment should be ignored
1768+
next line
1769+
# another comment
1770+
final line
1771+
another_key: works
1772+
YAML,
1773+
[
1774+
'key' => 'unquoted next line final line',
1775+
'another_key' => 'works',
1776+
],
1777+
];
1778+
1779+
yield 'only comments' => [
1780+
<<<YAML
1781+
key: unquoted
1782+
# this comment should be ignored
1783+
# another comment
1784+
another_key: works
1785+
YAML,
1786+
[
1787+
'key' => 'unquoted',
1788+
'another_key' => 'works',
1789+
],
1790+
];
1791+
1792+
yield 'blank lines and comments' => [
1793+
<<<YAML
1794+
key: unquoted
1795+
next line
1796+
1797+
# this comment should be ignored
1798+
final line
1799+
another_key: works
1800+
YAML,
1801+
[
1802+
'key' => "unquoted next line\nfinal line",
1803+
'another_key' => 'works',
1804+
],
1805+
];
1806+
1807+
yield 'comment at end' => [
1808+
<<<YAML
1809+
key: unquoted
1810+
next line
1811+
# comment at end
1812+
another_key: works
1813+
YAML,
1814+
[
1815+
'key' => 'unquoted next line',
1816+
'another_key' => 'works',
1817+
],
1818+
];
1819+
}
1820+
17541821
/**
17551822
* @dataProvider unquotedStringWithTrailingComment
17561823
*/

0 commit comments

Comments
 (0)