Skip to content

[Yaml] parse unquoted digits in tag values as integers #48333

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
Nov 25, 2022
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
3 changes: 1 addition & 2 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function parse(string $value = null, int $flags = 0, array &$refer
++$i;
break;
default:
$result = self::parseScalar($value, $flags, null, $i, null === $tag, $references);
$result = self::parseScalar($value, $flags, null, $i, true, $references);
}

// some comments are allowed at the end
Expand Down Expand Up @@ -657,7 +657,6 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
}

return octdec($value);
// Optimize for returning strings.
case \in_array($scalar[0], ['+', '-', '.'], true) || is_numeric($scalar[0]):
if (Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar)) {
$scalar = str_replace('_', '', $scalar);
Expand Down
9 changes: 1 addition & 8 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,6 @@ public function testDumpingTaggedValueSequenceWithInlinedTagValues()

YAML;
$this->assertSame($expected, $yaml);
// @todo Fix the parser, preserve numbers.
$data[2] = new TaggedValue('number', '5');
$this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
}

Expand Down Expand Up @@ -522,8 +520,6 @@ public function testDumpingTaggedValueMapRespectsInlineLevel()

YAML;
$this->assertSame($expected, $yaml);
// @todo Fix the parser, preserve numbers.
$data['count'] = new TaggedValue('number', '5');
$this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
}

Expand Down Expand Up @@ -577,9 +573,6 @@ public function testDumpingNotInlinedNullTaggedValue()
YAML;

$this->assertSame($expected, $this->dumper->dump($data, 2));

// @todo Fix the parser, don't stringify null.
$data['foo'] = new TaggedValue('bar', 'null');
$this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_CONSTANT));
}

Expand Down Expand Up @@ -696,7 +689,7 @@ public function testDumpMultiLineStringAsScalarBlock()
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }

YAML
);
);
$this->assertSame($expected, $yml);
$this->assertSame($data, $this->parser->parse($yml));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ yaml: |
no int: ! 12
string: !!str 12
php: |
[ 'integer' => 12, 'no int' => '12', 'string' => '12' ]
[ 'integer' => 12, 'no int' => 12, 'string' => '12' ]
---
test: Private types
todo: true
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,24 @@ public function testTagWithEmptyValueInMapping()
$this->assertSame('', $value['foo']->getValue());
}

public function testTagWithQuotedInteger()
{
$value = Inline::parse('!number "5"', Yaml::PARSE_CUSTOM_TAGS);

$this->assertInstanceOf(TaggedValue::class, $value);
$this->assertSame('number', $value->getTag());
$this->assertSame('5', $value->getValue());
}

public function testTagWithUnquotedInteger()
{
$value = Inline::parse('!number 5', Yaml::PARSE_CUSTOM_TAGS);

$this->assertInstanceOf(TaggedValue::class, $value);
$this->assertSame('number', $value->getTag());
$this->assertSame(5, $value->getValue());
}

public function testUnfinishedInlineMap()
{
$this->expectException(ParseException::class);
Expand All @@ -769,6 +787,7 @@ public function getTestsForOctalNumbers()

/**
* @group legacy
*
* @dataProvider getTestsForOctalNumbersYaml11Notation
*/
public function testParseOctalNumbersYaml11Notation(int $expected, string $yaml, string $replacement)
Expand Down
12 changes: 4 additions & 8 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ public function testTaggedValueTopLevelNumber()
{
$yml = '!number 5';
$data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
// @todo Preserve the number, don't turn into string.
$expected = new TaggedValue('number', '5');
$expected = new TaggedValue('number', 5);
$this->assertSameData($expected, $data);
}

public function testTaggedValueTopLevelNull()
{
$yml = '!tag null';
$data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
// @todo Preserve literal null, don't turn into string.
$expected = new TaggedValue('tag', 'null');
$this->assertSameData($expected, $data);

$this->assertSameData(new TaggedValue('tag', null), $data);
}

public function testTaggedValueTopLevelString()
Expand Down Expand Up @@ -1555,8 +1553,6 @@ public function testParseDateAsMappingValue()
}

/**
* @param $lineNumber
* @param $yaml
* @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
*/
public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
Expand Down Expand Up @@ -2310,7 +2306,7 @@ public function taggedValuesProvider()

public function testNonSpecificTagSupport()
{
$this->assertSame('12', $this->parser->parse('! 12'));
$this->assertSame(12, $this->parser->parse('! 12'));
}

public function testCustomTagsDisabled()
Expand Down