8000 [Yaml Parser] Fix edge cases when parsing multiple documents · symfony/symfony@012ee4f · GitHub
[go: up one dir, main page]

Skip to content

Commit 012ee4f

Browse files
committed
[Yaml Parser] Fix edge cases when parsing multiple documents
1 parent 6b8857c commit 012ee4f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function parse($value, $flags = 0)
153153
$this->refs = [];
154154
$this->skippedLineNumbers = [];
155155
$this->locallySkippedLineNumbers = [];
156+
$this->totalNumberOfLines = null;
156157

157158
if (null !== $e) {
158159
throw $e;

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

Lines changed: 33 additions & 0 deletions
< 8F21 td data-grid-cell-id="diff-e08f7eceb47aaa5984137cc6f3ea19b585dc972d3fafc005dd98234c04e60d2d-2364-2386-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,39 @@ public function testParseValueWithNegativeModifiers()
23622362
$this->parser->parse($yaml)
23632363
);
23642364
}
2365+
2366+
/**
2367+
* This is a regression test for a bug where a YAML block with a nested multiline string using | was parsed without
2368+
* a trailing \n when a shorter YAML document was parsed before.
2369+
*
2370+
* When a shorter document was parsed before, the nested string did not have a \n at the end of the string, because
2371+
* the Parser thought it was the end of the file, even though it is not.
2372+
*/
2373+
public function testParsingMultipleDocuments()
2374+
{
2375+
$shortDocument = 'foo: bar';
2376+
$longDocument = <<<YAML
2377+
a:
2378+
b: |
2379+
row
2380+
row2
2381+
c: d
2382+
YAML;
2383+
2384+
$expected = ['a' => ['b' => "row\nrow2\n"], 'c' => 'd'];
2385+
2386+
// The parser was not used before, so there is a new line after row2
2387+
$this->assertSame($expected, $this->parser->parse($longDocument));
2388+
2389+
$parser = new Parser();
2390+
// The first parsing set and fixed the totalNumberOfLines in the Parser before, so parsing the short document here
2391+
// to reproduce the issue. If the issue would not have been fixed, the next assertion will fail
2392+
$parser->parse($shortDocument);
2393+
2394+
// After the total number of lines has been rset the result will be the same as if a new parser was used
2395+
// (before, there was no \n after row2)
2396+
$this->assertSame($expected, $parser->parse($longDocument));
2397+
}
23652398
}
23662399

23672400
class B

0 commit comments

Comments
 (0)
0