Skip to content

Commit

Permalink
bug #26387 [Yaml] Fix regression when trying to parse multiline (anto…
Browse files Browse the repository at this point in the history
…grassiot)

This PR was squashed before being merged into the 3.4 branch (closes #26387).

Discussion
----------

[Yaml] Fix regression when trying to parse multiline

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |  #22967
| License       | MIT
| Doc PR        |

I think this is the lower impacted branch, it fixes a regression added in the last releases campain some days ago.

As discused on Slack @xabbuh

Commits
-------

e787ecf [Yaml] Fix regression when trying to parse multiline
  • Loading branch information
fabpot committed Apr 3, 2018
2 parents 3f316e5 + e787ecf commit ea69cc2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -402,7 +402,7 @@ private function doParse($value, $flags)
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
}

if (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1]) {
if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) {
@trigger_error($this->getDeprecationMessage('Starting an unquoted string with a question mark followed by a space is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.'), E_USER_DEPRECATED);
}

Expand All @@ -427,6 +427,10 @@ private function doParse($value, $flags)
$value = '';

foreach ($this->lines as $line) {
// If the indentation is not consistent at offset 0, it is to be considered as a ParseError
if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) {
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
}
if ('' === trim($line)) {
$value .= "\n";
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -812,6 +812,41 @@ public function testNonStringFollowedByCommentEmbeddedInMapping()
$this->assertSame($expected, $this->parser->parse($yaml));
}

public function getParseExceptionNotAffectedMultiLineStringLastResortParsing()
{
$tests = array();

$yaml = <<<'EOT'
a
b:
EOT;
$tests['parse error on first line'] = array($yaml);

$yaml = <<<'EOT'
a
b
c:
EOT;
$tests['parse error due to inconsistent indentation'] = array($yaml);

$yaml = <<<'EOT'
& * ! | > ' " % @ ` #, { asd a;sdasd }-@^qw3
EOT;
$tests['symfony/symfony/issues/22967#issuecomment-322067742'] = array($yaml);

return $tests;
}

/**
* @dataProvider getParseExceptionNotAffectedMultiLineStringLastResortParsing
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseExceptionNotAffectedByMultiLineStringLastResortParsing($yaml)
{
$this->parser->parse($yaml);
}

public function testMultiLineStringLastResortParsing()
{
$yaml = <<<'EOT'
Expand All @@ -825,6 +860,17 @@ public function testMultiLineStringLastResortParsing()
);

$this->assertSame($expected, $this->parser->parse($yaml));

$yaml = <<<'EOT'
a:
b
c
EOT;
$expected = array(
'a' => 'b c',
);

$this->assertSame($expected, $this->parser->parse($yaml));
}

/**
Expand Down

0 comments on commit ea69cc2

Please sign in to comment.