8000 [Yaml] support YAML 1.2 octal notation, deprecate YAML 1.1 one by xabbuh · Pull Request #34813 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] support YAML 1.2 octal notation, deprecate YAML 1.1 one #34813

8000 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
May 5, 2020
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
16 changes: 16 additions & 0 deletions UPGRADE-5.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,20 @@ Security
Yaml
----

* Added support for parsing numbers prefixed with `0o` as octal numbers.
* Deprecated support for parsing numbers starting with `0` as octal numbers. They will be parsed as strings as of Symfony 6.0. Prefix numbers with `0o`
so that they are parsed as octal numbers.

Before:

```yaml
Yaml::parse('072');
```

After:

```yaml
Yaml::parse('0o72');
```

* Deprecated using the `!php/object` and `!php/const` tags without a value.
16 changes: 16 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,20 @@ Security
Yaml
----

* Added support for parsing numbers prefixed with `0o` as octal numbers.
* Removed support for parsing numbers starting with `0` as octal numbers. They will be parsed as strings. Prefix numbers with `0o`
so that they are parsed as octal numbers.

Before:

```yaml
Yaml::parse('072');
```

After:

```yaml
Yaml::parse('0o72');
```

* Removed support for using the `!php/object` and `!php/const` tags without a value.
16 changes: 16 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ CHANGELOG
5.1.0
-----

* Added support for parsing numbers prefixed with `0o` as octal numbers.
* Deprecated support for parsing numbers starting with `0` as octal numbers. They will be parsed as strings as of Symfony 6.0. Prefix numbers with `0o`
so that they are parsed as octal numbers.

Before:

```yaml
Yaml::parse('072');
```

After:

```yaml
Yaml::parse('0o72');
```

* Added `yaml-lint` binary.
* Deprecated using the `!php/object` and `!php/const` tags without a value.

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,14 @@ private static function evaluateScalar(string $scalar, int $flags, array $refere
default:
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar), self::$parsedLineNumber, $scalar, self::$parsedFilename);
}
case preg_match('/^(?:\+|-)?0o(?P<value>[0-7_]++)$/', $scalar, $matches):
$value = str_replace('_', '', $matches['value']);

if ('-' === $scalar[0]) {
return -octdec($value);
} else {
return octdec($value);
}

// Optimize for returning strings.
// no break
Expand All @@ -644,11 +652,19 @@ private static function evaluateScalar(string $scalar, int $flags, array $refere
$raw = $scalar;
$cast = (int) $scalar;

if ('0' === $scalar[0] && '0' !== $scalar) {
trigger_deprecation('symfony/yaml', '5.1', 'Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0.');
}

return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
$raw = $scalar;
$cast = (int) $scalar;

if ('0' === $scalar[1] && '-0' !== $scalar) {
trigger_deprecation('symfony/yaml', '5.1', 'Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0.');
}

return '0' == $scalar[1] ? -octdec(substr($scalar, 1)) : (($raw === (string) $cast) ? $cast : $raw);
case is_numeric($scalar):
case Parser::preg_match(self::getHexRegex(), $scalar):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ test: Integers
spec: 2.19
yaml: |
canonical: 12345
octal: 014
octal: 0o14
hexadecimal: 0xC
php: |
[
Expand Down Expand Up @@ -1501,7 +1501,7 @@ ruby: |
test: Integer
yaml: |
canonical: 12345
octal: 014
octal: 0o14
hexadecimal: 0xC
php: |
[
Expand Down
7 changes: 0 additions & 7 deletions src/Symfony/Component/Yaml/Tests/Fixtures/sfTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ yaml: |
php: |
['foo', ['bar' => ['bar' => 'foo']]]
---
test: Octal
brief: as in spec example 2.19, octal value is converted
yaml: |
foo: 0123
php: |
['foo' => 83]
---
test: Octal strings
brief: Octal notation in a string must remain a string
yaml: |
Expand Down
27 changes: 24 additions & 3 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ public function getTestsForParse()
['123.45_67', 123.4567],
['0x4D2', 0x4D2],
['0x_4_D_2_', 0x4D2],
['02333', 02333],
['0_2_3_3_3', 02333],
['0o2333', 02333],
['0o_2_3_3_3', 02333],
['.Inf', -log(0)],
['-.Inf', log(0)],
["'686e444'", '686e444'],
Expand Down Expand Up @@ -379,7 +379,7 @@ public function getTestsForParseWithMapObjects()
["'quoted string'", 'quoted string'],
['12.30e+02', 12.30e+02],
['0x4D2', 0x4D2],
['02333', 02333],
['0o2333', 02333],
['.Inf', -log(0)],
['-.Inf', log(0)],
["'686e444'", '686e444'],
Expand Down Expand Up @@ -734,9 +734,30 @@ public function testParseOctalNumbers($expected, $yaml)
}

public function getTestsForOctalNumbers()
{
return [
'positive octal number' => [28, '0o34'],
'positive octal number with sign' => [28, '+0o34'],
'negative octal number' => [-28, '-0o34'],
];
}

/**
* @group legacy
* @dataProvider getTestsForOctalNumbersYaml11Notation
*/
public function testParseOctalNumbersYaml11Notation(int $expected, string $yaml)
{
$this->expectDeprecation('Since symfony/yaml 5.1: Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0.');

self::assertSame($expected, Inline::parse($yaml));
}

public function getTestsForOctalNumbersYaml11Notation()
{
return [
'positive octal number' => [28, '034'],
'positive octal number with separator' => [1243, '0_2_3_3_3'],
'negative octal number' => [-28, '-034'],
];
}
Expand Down
0