10000 bug #26387 [Yaml] Fix regression when trying to parse multiline (anto… · symfony/symfony@ea69cc2 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea69cc2

Browse files
committed
bug #26387 [Yaml] Fix regression when trying to parse multiline (antograssiot)
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
2 parents 3f316e5 + e787ecf commit ea69cc2

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ private function doParse($value, $flags)
402402
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
403403
}
404404

405-
if (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1]) {
405+
if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) {
406406
@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);
407407
}
408408

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

429429
foreach ($this->lines as $line) {
430+
// If the indentation is not consistent at offset 0, it is to be considered as a ParseError
431+
if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) {
432+
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
433+
}
430434
if ('' === trim($line)) {
431435
$value .= "\n";
432436
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,41 @@ public function testNonStringFollowedByCommentEmbeddedInMapping()
812812
$this->assertSame($expected, $this->parser->parse($yaml));
813813
}
814814

815+
public function getParseExceptionNotAffectedMultiLineStringLastResortParsing()
816+
{
817+
$tests = array();
818+
819+
$yaml = <<<'EOT'
820+
a
821+
b:
822+
EOT;
823+
$tests['parse error on first line'] = array($yaml);
824+
825+
$yaml = <<<'EOT'
826+
a
827+
828+
b
829+
c:
830+
EOT;
831+
$tests['parse error due to inconsistent indentation'] = array($yaml);
832+
833+
$yaml = <<<'EOT'
834+
& * ! | > ' " % @ ` #, { asd a;sdasd }-@^qw3
835+
EOT;
836+
$tests['symfony/symfony/issues/22967#issuecomment-322067742'] = array($yaml);
837+
838+
return $tests;
839+
}
840+
841+
/**
842+
* @dataProvider getParseExceptionNotAffectedMultiLineStringLastResortParsing
843+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
844+
*/
845+
public function testParseExceptionNotAffectedByMultiLineStringLastResortParsing($yaml)
846+
{
847+
$this->parser->parse($yaml);
848+
}
849+
815850
public function testMultiLineStringLastResortParsing()
816851
{
817852
$yaml = <<<'EOT'
@@ -825,6 +860,17 @@ public function testMultiLineStringLastResortParsing()
825860
);
826861

827862
$this->assertSame($expected, $this->parser->parse($yaml));
863+
864+
$yaml = <<<'EOT'
865+
a:
866+
b
867+
c
868+
EOT;
869+
$expected = array(
870+
'a' => 'b c',
871+
);
872+
873+
$this->assertSame($expected, $this->parser->parse($yaml));
828874
}
829875

830876
/**

0 commit comments

Comments
 (0)
0