Skip to content

Commit 7bcccef

Browse files
committed
bug symfony#25787 Yaml parser regression with comments and non-strings (alexpott)
This PR was squashed before being merged into the 3.4 branch (closes symfony#25787). Discussion ---------- Yaml parser regression with comments and non-strings | Q | A | ------------- | --- | Branch? | 3.3 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | no | Fixed tickets | symfony#25786 | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. - Replace this comment by a description of what your PR is solving. --> Commits ------- a7e2a49 Yaml parser regression with comments and non-strings
2 parents 9041ec3 + a7e2a49 commit 7bcccef

File tree

2 files changed

+58
-37
lines changed

2 files changed

+58
-37
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -422,54 +422,39 @@ private function doParse($value, $flags)
422422

423423
// try to parse the value as a multi-line string as a last resort
424424
if (0 === $this->currentLineNb) {
425-
$parseError = false;
426425
$previousLineWasNewline = false;
427426
$previousLineWasTerminatedWithBackslash = false;
428427
$value = '';
429428

430429
foreach ($this->lines as $line) {
431-
try {
432-
if (isset($line[0]) && ('"' === $line[0] || "'" === $line[0])) {
433-
$parsedLine = $line;
434-
} else {
435-
$parsedLine = Inline::parse($line, $flags, $this->refs);
436-
}
437-
438-
if (!is_string($parsedLine)) {
439-
$parseError = true;
440-
break;
441-
}
442-
443-
if ('' === trim($parsedLine)) {
444-
$value .= "\n";
445-
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
446-
$value .= ' ';
447-
}
430+
if ('' === trim($line)) {
431+
$value .= "\n";
432+
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
433+
$value .= ' ';
434+
}
448435

449-
if ('' !== trim($parsedLine) && '\\' === substr($parsedLine, -1)) {
450-
$value .= ltrim(substr($parsedLine, 0, -1));
451-
} elseif ('' !== trim($parsedLine)) {
452-
$value .= trim($parsedLine);
453-
}
436+
if ('' !== trim($line) && '\\' === substr($line, -1)) {
437+
$value .= ltrim(substr($line, 0, -1));
438+
} elseif ('' !== trim($line)) {
439+
$value .= trim($line);
440+
}
454441

455-
if ('' === trim($parsedLine)) {
456-
$previousLineWasNewline = true;
457-
$previousLineWasTerminatedWithBackslash = false;
458-
} elseif ('\\' === substr($parsedLine, -1)) {
459-
$previousLineWasNewline = false;
460-
$previousLineWasTerminatedWithBackslash = true;
461-
} else {
462-
$previousLineWasNewline = false;
463-
$previousLineWasTerminatedWithBackslash = false;
464-
}
465-
} catch (ParseException $e) {
466-
$parseError = true;
467-
break;
442+
if ('' === trim($line)) {
443+
$previousLineWasNewline = true;
444+
$previousLineWasTerminatedWithBackslash = false;
445+
} elseif ('\\' === substr($line, -1)) {
446+
$previousLineWasNewline = false;
447+
$previousLineWasTerminatedWithBackslash = true;
448+
} else {
449+
$previousLineWasNewline = false;
450+
$previousLineWasTerminatedWithBackslash = false;
468451
}
469452
}
470453

471-
if (!$parseError) {
454+
try {
472455
return Inline::parse(trim($value));
456+
} catch (ParseException $e) {
457+
// fall-through to the ParseException thrown below
473458
}
474459
}
475460

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,42 @@ public function testSequenceFollowedByCommentEmbeddedInMapping()
791791
$this->assertSame($expected, $this->parser->parse($yaml));
792792
}
793793

794+
public function testNonStringFollowedByCommentEmbeddedInMapping()
795+
{
796+
$yaml = <<<'EOT'
797+
a:
798+
b:
799+
{}
800+
# comment
801+
d:
802+
1.1
803+
# another comment
804+
EOT;
805+
$expected = array(
806+
'a' => array(
807+
'b' => array(),
808+
'd' => 1.1,
809+
),
810+
);
811+
812+
$this->assertSame($expected, $this->parser->parse($yaml));
813+
}
814+
815+
public function testMultiLineStringLastResortParsing()
816+
{
817+
$yaml = <<<'EOT'
818+
test:
819+
You can have things that don't look like strings here
820+
true
821+
yes you can
822+
EOT;
823+
$expected = array(
824+
'test' => 'You can have things that don\'t look like strings here true yes you can',
825+
);
826+
827+
$this->assertSame($expected, $this->parser->parse($yaml));
828+
}
829+
794830
/**
795831
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
796832
*/

0 commit comments

Comments
 (0)