Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[YAML] Unexpected characters using YAML compact syntax #37788
  • Loading branch information
RevZer0 committed Aug 18, 2020
commit 24fc166f8375e71316c80fbaff66586700d2326e
38 changes: 26 additions & 12 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Parser
private $skippedLineNumbers = [];
private $locallySkippedLineNumbers = [];
private $refsBeingParsed = [];
private $openMappingCount = 0;

/**
* Parses a YAML file into a PHP value.
Expand Down Expand Up @@ -1208,18 +1209,12 @@ private function lexInlineMapping(string $yaml): string
if ('' === $yaml || '{' !== $yaml[0]) {
throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml));
}

for ($i = 1; isset($yaml[$i]) && '}' !== $yaml[$i]; ++$i) {
}

if (isset($yaml[$i]) && '}' === $yaml[$i]) {
return $yaml;
}

$lines = [$yaml];

while ($this->moveToNextLine()) {
$lines[] = $this->currentLine;
$this->openMappingCount = 0;
$lines = [
$this->calculateLineOpenMappings($yaml)
];
while (!$this->allMappingIsClosed() && $this->moveToNextLine()) {
$lines[] = $this->calculateLineOpenMappings($this->currentLine);
}

return implode("\n", $lines);
Expand Down Expand Up @@ -1253,4 +1248,23 @@ private function lexInlineSequence(string $yaml): string

return $value;
}

private function allMappingIsClosed(): bool
{
return 0 === $this->openMappingCount;
}

private function calculateLineOpenMappings(string $yamlLine): string
{
for ($i = 0; isset($yamlLine[$i]); ++$i) {
if ('{' === $yamlLine[$i]) {
++$this->openMappingCount;
}
if ('}' === $yamlLine[$i]) {
--$this->openMappingCount;
}
}

return trim($yamlLine);
}
}
47 changes: 47 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,53 @@ public function testParseValueWithNegativeModifiers()
$this->parser->parse($yaml)
);
}

/**
* Test that covers #37788 issue
*
* @dataProvider validMappingSequenceProvider
*/
public function testParametersAfterMappingSequence(string $yaml, array $parsed): void
{
self::assertSame($parsed, $this->parser->parse($yaml));
}

public function validMappingSequenceProvider(): iterable
{
$expected = [
'map' => [
'key' => 'value',
'a' => 'b'
],
'param' => 'some'
];

yield "multiline syntax" => [
<<<YAML
map: {
key: "value",
a: "b"
}
param: "some"
YAML,
$expected
];
yield "inline syntax" => [
<<<YAML
map: {key: "value", a: "b"}
param: "some"
YAML,
$expected
];
yield "mixed syntax" => [
<<<YAML
map: {key: "value",
a: "b"}
param: "some"
YAML,
$expected
];
}
}

class B
Expand Down