Skip to content

Commit bb77914

Browse files
committed
bug #36683 [Yaml] fix parse error when unindented collections contain a comment (wdiesveld)
This PR was squashed before being merged into the 3.4 branch. Discussion ---------- [Yaml] fix parse error when unindented collections contain a comment | Q | A | ------------- | --- | Branch? | 5.0 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #36558 | License | MIT ### Problem The method `Parser::getNextEmbedBlock` did not determine the yaml-block correctly when there was a comment before the first unindented collection item. This was caused by the fact that the check for unindented collection items was done for the _first line of the block only_. So in case this first line is a comment, this check will result in _false_, while in fact the parser is in an unindented collection. ### Solution In the solution I implemented the parser will check for comment lines as well. As long as the loop encounters a comment line, it will check (in the next iteration) whether the line is an unindented collection item. So this check will be done until all comments before the first uncommented item are parsed. Commits ------- 58bb2c5 [Yaml] fix parse error when unindented collections contain a comment
2 parents 469d82d + 58bb2c5 commit bb77914

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,14 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
619619
}
620620

621621
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
622+
$isItComment = $this->isCurrentLineComment();
622623

623624
while ($this->moveToNextLine()) {
625+
if ($isItComment && !$isItUnindentedCollection) {
626+
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
627+
$isItComment = $this->isCurrentLineComment();
628+
}
629+
624630
$indent = $this->getCurrentLineIndentation();
625631

626632
if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {

src/Symfony/Component/Yaml/Tests/Fixtures/sfComments.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,17 @@ yaml: |
7474
'foo #': baz
7575
php: |
7676
['foo #' => 'baz']
77+
---
78+
test: Comment before first item in unindented collection
79+
brief: >
80+
Comment directly before unindented collection is allowed
81+
yaml: |
82+
collection1:
83+
# comment
84+
- a
85+
- b
86+
collection2:
87+
- a
88+
- b
89+
php: |
90+
['collection1' => ['a', 'b'], 'collection2' => ['a', 'b']]

0 commit comments

Comments
 (0)