8000 bug #11799 [YAML] fix handling of empty sequence items (xabbuh) · alexpott/symfony@f12890c · GitHub
[go: up one dir, main page]

Skip to content

Commit f12890c

Browse files
committed
bug symfony#11799 [YAML] fix handling of empty sequence items (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [YAML] fix handling of empty sequence items | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#11798 | License | MIT | Doc PR | When a line contains only a dash it cannot safely be assumed that it contains a nested list or an embedded mapping. If the next line starts with a dash at the same indentation, the current line's item is to be treated as `null`. Commits ------- fc85435 fix handling of empty sequence items
2 parents a41c16c + fc85435 commit f12890c

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
9393
$c = $this->getRealCurrentLineNb() + 1;
9494
$parser = new Parser($c);
9595
$parser->refs =& $this->refs;
96-
$data[] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
96+
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport);
9797
} else {
9898
if (isset($values['leadspaces'])
9999
&& ' ' == $values['leadspaces']
@@ -281,15 +281,20 @@ private function getCurrentLineIndentation()
281281
/**
282282
* Returns the next embed block of YAML.
283283
*
284-
* @param int $indentation The indent level at which the block is to be read, or null for default
284+
* @param int $indentation The indent level at which the block is to be read, or null for default
285+
* @param bool $inSequence True if the enclosing data structure is a sequence
285286
*
286287
* @return string A YAML string
287288
*
288289
* @throws ParseException When indentation problem are detected
289290
*/
290-
private function getNextEmbedBlock($indentation = null)
291+
private function getNextEmbedBlock($indentation = null, $inSequence = false)
291292
{
292-
$this->moveToNextLine();
293+
$oldLineIndentation = $this->getCurrentLineIndentation();
294+
295+
if (!$this->moveToNextLine()) {
296+
return;
297+
}
293298

294299
if (null === $indentation) {
295300
$newIndent = $this->getCurrentLineIndentation();
@@ -305,6 +310,14 @@ private function getNextEmbedBlock($indentation = null)
305310

306311
$data = array(substr($this->currentLine, $newIndent));
307312

313+
if ($inSequence && $oldLineIndentation === $newIndent && '-' === $data[0][0]) {
314+
// the previous line contained a dash but no item content, this line is a sequence item with the same indentation
315+
// and therefore no nested list or mapping
316+
$this->moveToPreviousLine();
317+
318+
return;
319+
}
320+
308321
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
309322

310323
// Comments must not be removed inside a string block (ie. after a line ending with "|")

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

Lines changed: 24 additions & 0 deleti EA8E ons
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ yaml: |
1111
php: |
1212
array('apple', 'banana', 'carrot')
1313
---
14+
test: Sequence With Item Being Null In The Middle
15+
brief: |
16+
You can specify a list in YAML by placing each
17+
member of the list on a new line with an opening
18+
dash. These lists are called sequences.
19+
yaml: |
20+
- apple
21+
-
22+
- carrot
23+
php: |
24+
array('apple', null, 'carrot')
25+
---
26+
test: Sequence With Last Item Being Null
27+
brief: |
28+
You can specify a list in YAML by placing each
29+
member of the list on a new line with an opening
30+
dash. These lists are called sequences.
31+
yaml: |
32+
- apple
33+
- banana
34+
-
35+
php: |
36+
array('apple', 'banana', null)
37+
---
1438
test: Nested Sequences
1539
brief: |
1640
You can include a sequence within another

0 commit comments

Comments
 (0)
0