From 012ee4fa59549c64320b4cfe1bea166e9bd30ae3 Mon Sep 17 00:00:00 2001 From: Markus Fasselt Date: Fri, 18 Sep 2020 10:16:56 +0200 Subject: [PATCH] [Yaml Parser] Fix edge cases when parsing multiple documents --- src/Symfony/Component/Yaml/Parser.php | 1 + .../Component/Yaml/Tests/ParserTest.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 9cfd1d45cb..ddd85676ea 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -153,6 +153,7 @@ class Parser $this->refs = []; $this->skippedLineNumbers = []; $this->locallySkippedLineNumbers = []; + $this->totalNumberOfLines = null; if (null !== $e) { throw $e; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index b6e6a1141b..883d2c0c3d 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -2362,6 +2362,39 @@ YAML; $this->parser->parse($yaml) ); } + + /** + * This is a regression test for a bug where a YAML block with a nested multiline string using | was parsed without + * a trailing \n when a shorter YAML document was parsed before. + * + * When a shorter document was parsed before, the nested string did not have a \n at the end of the string, because + * the Parser thought it was the end of the file, even though it is not. + */ + public function testParsingMultipleDocuments() + { + $shortDocument = 'foo: bar'; + $longDocument = << ['b' => "row\nrow2\n"], 'c' => 'd']; + + // The parser was not used before, so there is a new line after row2 + $this->assertSame($expected, $this->parser->parse($longDocument)); + + $parser = new Parser(); + // The first parsing set and fixed the totalNumberOfLines in the Parser before, so parsing the short document here + // to reproduce the issue. If the issue would not have been fixed, the next assertion will fail + $parser->parse($shortDocument); + + // After the total number of lines has been rset the result will be the same as if a new parser was used + // (before, there was no \n after row2) + $this->assertSame($expected, $parser->parse($longDocument)); + } } class B