Skip to content

[Yaml] deprecate "? " starting unquoted strings #22059

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

Merged
merged 1 commit into from
Mar 20, 2017
Merged
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
8 changes: 6 additions & 2 deletions UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,12 @@ Workflow
Yaml
----

* Deprecated support for implicitly parsing non-string mapping keys as strings. Mapping keys that are no strings will
lead to a `ParseException` in Symfony 4.0. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as
* Starting an unquoted string with a question mark followed by a space is
deprecated and will throw a `ParseException` in Symfony 4.0.

* Deprecated support for implicitly parsing non-string mapping keys as strings.
Mapping keys that are no strings will lead to a `ParseException` in Symfony
4.0. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as
strings.

Before:
Expand Down
8 changes: 6 additions & 2 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,12 @@ Workflow
Yaml
----

* Removed support for implicitly parsing non-string mapping keys as strings. Mapping keys that are no strings will
result in a `ParseException`. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as strings.
* Starting an unquoted string with a question mark followed by a space
throws a `ParseException`.

* Removed support for implicitly parsing non-string mapping keys as strings.
Mapping keys that are no strings will result in a `ParseException`. Use the
`PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as strings.

Before:

Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ CHANGELOG
3.3.0
-----

* Deprecated support for implicitly parsing non-string mapping keys as strings. Mapping keys that are no strings will
lead to a `ParseException` in Symfony 4.0. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as
* Starting an unquoted string with a question mark followed by a space is
deprecated and will throw a `ParseException` in Symfony 4.0.

* Deprecated support for implicitly parsing non-string mapping keys as strings.
Mapping keys that are no strings will lead to a `ParseException` in Symfony
4.0. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as
strings.

Before:
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public function parse($value, $flags = 0)
$values['value'] = $matches['value'];
}

if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) {
@trigger_error('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', E_USER_DEPRECATED);
}

// array
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags);
Expand Down Expand Up @@ -301,6 +305,10 @@ public function parse($value, $flags = 0)
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine);
}

if (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1]) {
@trigger_error('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', E_USER_DEPRECATED);
}

// 1-liner optionally followed by newline(s)
if (is_string($value) && $this->lines[0] === trim($value)) {
try {
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,52 @@ public function testExceptionWhenUsingUnsuportedBuiltInTags()
$this->parser->parse('!!foo');
}

/**
* @group legacy
* @expectedDeprecation Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.
*/
public function testComplexMappingThrowsParseException()
{
$yaml = <<<YAML
? "1"
:
name: végétalien
YAML;

$this->parser->parse($yaml);
}

/**
* @group legacy
* @expectedDeprecation Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.
*/
public function testComplexMappingNestedInMappingThrowsParseException()
{
$yaml = <<<YAML
diet:
? "1"
:
name: végétalien
YAML;

$this->parser->parse($yaml);
}

/**
* @group legacy
* @expectedDeprecation Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.
*/
public function testComplexMappingNestedInSequenceThrowsParseException()
{
$yaml = <<<YAML
- ? "1"
:
name: végétalien
YAML;

$this->parser->parse($yaml);
}

private function loadTestsFromFixtureFiles($testsFile)
{
$parser = new Parser();
Expand Down