8000 [Yaml] fixed parser incorrectly parses some yaml block sequences · symfony/symfony@6817261 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 6817261

Browse files
committed
[Yaml] fixed parser incorrectly parses some yaml block sequences
1 parent 5dd56a6 commit 6817261

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 12 additions & 6 deletions
+
* @param bool $isInlineFirstChild
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ private function doParse(string $value, int $flags)
169169
}
170170

171171
// array
172-
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
172+
if (isset($values['value']) && 0 === strpos(ltrim($values['value'], ' '), '-')) {
173+
// Inline first child
174+
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(2, true, true) ?? '', $flags);
175+
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
173176
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
174177
} elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
175178
$data[] = new TaggedValue(
@@ -558,16 +561,17 @@ private function getCurrentLineIndentation(): int
558561
*
559562
* @param int|null $indentation The indent level at which the block is to be read, or null for default
560563
* @param bool $inSequence True if the enclosing data structure is a sequence
564
561565
*
562566
* @return string A YAML string
563567
*
564568
* @throws ParseException When indentation problem are detected
565569
*/
566-
private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): string
570+
private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false, $isInlineFirstChild = false): string
567571
{
568572
$oldLineIndentation = $this->getCurrentLineIndentation();
569573

570-
if (!$this->moveToNextLine()) {
574+
if (!$isInlineFirstChild && !$this->moveToNextLine()) {
571575
return '';
572576
}
573577

@@ -604,7 +608,9 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f
604608
}
605609

606610
$data = [];
607-
if ($this->getCurrentLineIndentation() >= $newIndent) {
611+
if ($isInlineFirstChild) {
612+
$data[] = substr($this->currentLine, $newIndent);
613+
} elseif ($this->getCurrentLineIndentation() >= $newIndent) {
608614
$data[] = substr($this->currentLine, $newIndent);
609615
} elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
610616
$data[] = $this->currentLine;
@@ -614,7 +620,7 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f
614620
return '';
615621
}
616622

617-
if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
623+
if (!$isInlineFirstChild && $inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
618624
// the previous line contained a dash but no item content, this line is a sequence item with the same indentation
619625
// and therefore no nested list or mapping
620626
$this->moveToPreviousLine();
@@ -633,7 +639,7 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f
633639

634640
$indent = $this->getCurrentLineIndentation();
635641

636-
if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
642+
if (!$isInlineFirstChild && $isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
637643
$this->moveToPreviousLine();
638644
break;
639645
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,41 @@ php: |
200200
'age' => 38
201201
]
202202
]
203+
---
204+
test: Block Sequence in Block Sequence 1
205+
brief: |
206+
Block Sequence in Block Sequence 1
207+
yaml: |
208+
- - s1_i1
209+
- s1_i2
210+
- s2
211+
php: |
212+
[
213+
[
214+
"s1_i1",
215+
"s1_i2"
216+
],
217+
"s2"
218+
]
219+
---
220+
test: Block Sequence in Block Sequence 2
221+
brief: |
222+
Block Sequence in Block Sequence 2
223+
yaml: |
224+
- - s1_i1
225+
- - s1_i1_1
226+
- s1_i1_2
227+
- s1_i2
228+
- s2
229+
php: |
230+
[
231+
[
232+
"s1_i1",
233+
[
234+
"s1_i1_1",
235+
"s1_i1_2",
236+
],
237+
"s1_i2"
238+
],
239+
"s2"
240+
]

0 commit comments

Comments
 (0)
0