Skip to content

[Yaml] fixed parser incorrectly parses some yaml block sequences #40500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ private function doParse(string $value, int $flags)
}

// array
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
if (isset($values['value']) && 0 === strpos(ltrim($values['value'], ' '), '-')) {
// Inline first child
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(2, true, true) ?? '', $flags);
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
} elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
$data[] = new TaggedValue(
Expand Down Expand Up @@ -556,18 +559,19 @@ private function getCurrentLineIndentation(): int
/**
* Returns the next embed block of YAML.
*
* @param int|null $indentation The indent level at which the block is to be read, or null for default
* @param bool $inSequence True if the enclosing data structure is a sequence
* @param int|null $indentation The indent level at which the block is to be read, or null for default
* @param bool $inSequence True if the enclosing data structure is a sequence
* @param bool $isInlineFirstChild True if the definition has an inline first element
*
* @return string A YAML string
*
* @throws ParseException When indentation problem are detected
*/
private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): string
private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false, $isInlineFirstChild = false): string
{
$oldLineIndentation = $this->getCurrentLineIndentation();

if (!$this->moveToNextLine()) {
if (!$isInlineFirstChild && !$this->moveToNextLine()) {
return '';
}

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

$data = [];
if ($this->getCurrentLineIndentation() >= $newIndent) {
if ($isInlineFirstChild) {
$data[] = substr($this->currentLine, $newIndent);
} elseif ($this->getCurrentLineIndentation() >= $newIndent) {
$data[] = substr($this->currentLine, $newIndent);
} elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
$data[] = $this->currentLine;
Expand All @@ -614,7 +620,7 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f
return '';
}

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

$indent = $this->getCurrentLineIndentation();

if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
if (!$isInlineFirstChild && $isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
$this->moveToPreviousLine();
break;
}
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Yaml/Tests/Fixtures/YtsBasicTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,41 @@ php: |
'age' => 38
]
]
---
test: Block Sequence in Block Sequence 1
brief: |
Block Sequence in Block Sequence 1
yaml: |
- - s1_i1
- s1_i2
- s2
php: |
[
[
"s1_i1",
"s1_i2"
],
"s2"
]
---
test: Block Sequence in Block Sequence 2
brief: |
Block Sequence in Block Sequence 2
yaml: |
- - s1_i1
- - s1_i1_1
- s1_i1_2
- s1_i2
- s2
php: |
[
[
"s1_i1",
[
"s1_i1_1",
"s1_i1_2",
],
"s1_i2"
],
"s2"
]