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

Skip to content

Commit dd6418a

Browse files
committed
[Yaml] deprecate unquoted indicator characters
1 parent 5805cc5 commit dd6418a

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
236236
throw new ParseException(sprintf('Malformed inline YAML string (%s).', $scalar));
237237
}
238238

239-
// a non-quoted string cannot start with @ or ` (reserved)
240-
if ($output && ('@' === $output[0] || '`' === $output[0])) {
239+
// a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
240+
if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0])) {
241241
@trigger_error(sprintf('Not quoting a scalar starting with "%s" is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $output[0]), E_USER_DEPRECATED);
242242

243243
// to be thrown in 3.0

src/Symfony/Component/Yaml/Parser.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
114114

115115
$data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap);
116116
} else {
117-
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
117+
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
118118
}
119119
}
120120
if ($isRef) {
@@ -230,7 +230,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
230230
}
231231
}
232232
} else {
233-
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
233+
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
234234
// Spec: Keys MUST be unique; first one wins.
235235
// But overwriting is allowed when a merge node is used in current block.
236236
if ($allowOverwrite || !isset($data[$key])) {
@@ -445,12 +445,13 @@ private function moveToPreviousLine()
445445
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
446446
* @param bool $objectSupport True if object support is enabled, false otherwise
447447
* @param bool $objectForMap true if maps should return a stdClass instead of array()
448+
* @param string $context The parser context (either sequence or mapping)
448449
*
449450
* @return mixed A PHP value
450451
*
451452
* @throws ParseException When reference does not exist
452453
*/
453-
private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap)
454+
private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context)
454455
{
455456
if (0 === strpos($value, '*')) {
456457
if (false !== $pos = strpos($value, '#')) {
@@ -472,6 +473,13 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
472473
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
473474
}
474475

476+
if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($value, ': ')) {
477+
@trigger_error('Using a colon in an unquoted mapping value is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', E_USER_DEPRECATED);
478+
479+
// to be thrown in 3.0
480+
// throw new ParseException('A colon cannot be used in an unquoted mapping value.');
481+
}
482+
475483
try {
476484
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
477485
} catch (ParseException $e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ test: Some characters at the beginning of a string must be escaped
33
brief: >
44
Some characters at the beginning of a string must be escaped
55
yaml: |
6-
foo: | bar
6+
foo: '| bar'
77
php: |
88
array('foo' => '| bar')
99
---

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ public function getReservedIndicators()
205205
return array(array('@'), array('`'));
206206
}
207207

208+
/**
209+
* @group legacy
210+
* @dataProvider getScalarIndicators
211+
* throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
212+
*/
213+
public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
214+
{
215+
Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
216+
}
217+
218+
public function getScalarIndicators()
219+
{
220+
return array(array('|'), array('>'));
221+
}
222+
208223
public function getTestsForParse()
209224
{
210225
return array(

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,19 @@ public function testFloatKeys()
783783

784784
$this->assertEquals($expected, $this->parser->parse($yaml));
785785
}
786+
787+
/**
788+
* @group legacy
789+
* throw ParseException in Symfony 3.0
790+
*/
791+
public function testColonInMappingValueException()
792+
{
793+
$yaml = <<<EOF
794+
foo: bar: baz
795+
EOF;
796+
797+
$this->parser->parse($yaml);
798+
}
786799
}
787800

788801
class B

0 commit comments

Comments
 (0)