Skip to content

[Yaml] fix parsing multi-line mapping values #19304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,29 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
}

try {
$quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;

// do not take following lines into account when the current line is a quoted single line value
if (null !== $quotation && preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
}

while ($this->moveToNextLine()) {
// unquoted strings end before the first unindented line
if (null === $quotation && $this->getCurrentLineIndentation() === 0) {
$this->moveToPreviousLine();

break;
}

$value .= ' '.trim($this->currentLine);

// quoted string values end with a line that is terminated with the quotation character
if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) {
break;
}
}

return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
} catch (ParseException $e) {
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,32 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider()
),
);
}

public function testParseMultiLineQuotedString()
{
$yaml = <<<EOT
foo: "bar
baz
foobar
foo"
bar: baz
EOT;

$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
}

public function testParseMultiLineUnquotedString()
{
$yaml = <<<EOT
foo: bar
baz
foobar
foo
bar: baz
EOT;

$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
}
}

class B
Expand Down