Skip to content

Commit

Permalink
bug #18899 [Yaml] search for colons in strings only (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.1 branch.

Discussion
----------

[Yaml] search for colons in strings only

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18897
| License       | MIT
| Doc PR        |

Since the parser is able to return `\DateTime` instances when the
`Yaml::PARSE_DATETIME` flag is passed, we need to ensure that the parsed
value actually is a string before passing the parsed value to string
functions.

Commits
-------

0ea2228 [Yaml] search for colons in strings only
  • Loading branch information
xabbuh committed May 28, 2016
2 parents 91a4de3 + 0ea2228 commit 922f1b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -539,7 +539,7 @@ private function parseValue($value, $flags, $context)
try {
$parsedValue = Inline::parse($value, $flags, $this->refs);

if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
throw new ParseException('A colon cannot be used in an unquoted mapping value.');
}

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -1230,6 +1230,19 @@ public function getInvalidBinaryData()
),
);
}

public function testParseDateAsMappingValue()
{
$yaml = <<<EOT
date: 2002-12-14
EOT;
$expectedDate = new \DateTime();
$expectedDate->setTimeZone(new \DateTimeZone('UTC'));
$expectedDate->setDate(2002, 12, 14);
$expectedDate->setTime(0, 0, 0);

$this->assertEquals(array('date' => $expectedDate), $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
}
}

class B
Expand Down

0 comments on commit 922f1b0

Please sign in to comment.