8000 support YAML 1.2 octal notation, deprecate YAML 1.1 one · symfony/symfony@500ef6b · GitHub
[go: up one dir, main page]

Skip to content

Commit 500ef6b

Browse files
committed
support YAML 1.2 octal notation, deprecate YAML 1.1 one
1 parent 0d4bba8 commit 500ef6b

File tree

7 files changed

+87
-12
lines changed

7 files changed

+87
-12
lines changed

UPGRADE-5.1.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,20 @@ Security
164164
Yaml
165165
----
166166

167+
* Added support for parsing numbers prefixed with `0o` as octal numbers.
168+
* 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`
169+
so that they are parsed as octal numbers.
170+
171+
Before:
172+
173+
```yaml
174+
Yaml::parse('072');
175+
```
176+
177+
After:
178+
179+
```yaml
180+
Yaml::parse('0o72');
181+
```
182+
167183
* Deprecated using the `!php/object` and `!php/const` tags without a value.

UPGRADE-6.0.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,20 @@ Security
111111
Yaml
112112
----
113113

114+
* Added support for parsing numbers prefixed with `0o` as octal numbers.
115+
* Removed support for parsing numbers starting with `0` as octal numbers. They will be parsed as strings. Prefix numbers with `0o`
116+
so that they are parsed as octal numbers.
117+
118+
Before:
119+
120+
```yaml
121+
Yaml::parse('072');
122+
```
123+
124+
After:
125+
126+
```yaml
127+
Yaml::parse('0o72');
128+
```
129+
114130
* Removed support for using the `!php/object` and `!php/const` tags without a value.

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ CHANGELOG
44
5.1.0
55
-----
66

7+
* Added support for parsing numbers prefixed with `0o` as octal numbers.
8+
* 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`
9+
so that they are parsed as octal numbers.
10+
11+
Before:
12+
13+
```yaml
14+
Yaml::parse('072');
15+
```
16+
17+
After:
18+
19+
```yaml
20+
Yaml::parse('0o72');
21+
```
22+
723
* Added `yaml-lint` binary.
824
* Deprecated using the `!php/object` and `!php/const` tags without a value.
925

src/Symfony/Component/Yaml/Inline.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,12 @@ private static function evaluateScalar(string $scalar, int $flags, array $refere
631631
default:
632632
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);
633633
}
634+
case preg_match('/^(?:\+|-)?0o(?P<value>[0-7]+)$/', $scalar, $matches):
635+
if ('-' === $scalar[0]) {
636+
return -octdec($matches['value']);
637+
} else {
638+
return octdec($matches['value']);
639+
}
634640

635641
// Optimize for returning strings.
636642
// no break
@@ -644,11 +650,19 @@ private static function evaluateScalar(string $scalar, int $flags, array $refere
644650
$raw = $scalar;
645651
$cast = (int) $scalar;
646652

653+
if ('0' === $scalar[0] && '0' !== $scalar) {
654+
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.');
655+
}
656+
647657
return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
648658
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
649659
$raw = $scalar;
650660
$cast = (int) $scalar;
651661

662+
if ('0' === $scalar[1] && '-0' !== $scalar) {
663+
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.');
664+
}
665+
652666
return '0' == $scalar[1] ? -octdec(substr($scalar, 1)) : (($raw === (string) $cast) ? $cast : $raw);
653667
case is_numeric($scalar):
654668
case Parser::preg_match(self::getHexRegex(), $scalar):

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ test: Integers
509509
spec: 2.19
510510
yaml: |
511511
canonical: 12345
512-
octal: 014
512+
octal: 0o14
513513
hexadecimal: 0xC
514514
php: |
515515
[
@@ -1501,7 +1501,7 @@ ruby: |
15011501
test: Integer
15021502
yaml: |
15031503
canonical: 12345
1504-
octal: 014
1504+
octal: 0o14
15051505
hexadecimal: 0xC
15061506
php: |
15071507
[

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ yaml: |
9696
php: |
9797
['foo', ['bar' => ['bar' => 'foo']]]
9898
---
99-
test: Octal
100-
brief: as in spec example 2.19, octal value is converted
101-
yaml: |
102-
foo: 0123
103-
php: |
104-
['foo' => 83]
105-
---
10699
test: Octal strings
107100
brief: Octal notation in a string must remain a string
108101
yaml: |

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ public function getTestsForParse()
299299
['123.45_67', 123.4567],
300300
['0x4D2', 0x4D2],
301301
['0x_4_D_2_', 0x4D2],
302-
['02333', 02333],
303-
['0_2_3_3_3', 02333],
302+
['0o2333', 02333],
304303
['.Inf', -log(0)],
305304
['-.Inf', log(0)],
306305
["'686e444'", '686e444'],
@@ -379,7 +378,7 @@ public function getTestsForParseWithMapObjects()
379378
["'quoted string'", 'quoted string'],
380379
['12.30e+02', 12.30e+02],
381380
['0x4D2', 0x4D2],
382-
['02333', 02333],
381+
['0o2333', 02333],
383382
['.Inf', -log(0)],
384383
['-.Inf', log(0)],
385384
["'686e444'", '686e444'],
@@ -734,9 +733,30 @@ public function testParseOctalNumbers($expected, $yaml)
734733
}
735734

736735
public function getTestsForOctalNumbers()
736+
{
737+
return [
738+
'positive octal number' => [28, '0o34'],
739+
'positive octal number with sign' => [28, '+0o34'],
740+
'negative octal number' => [-28, '-0o34'],
741+
];
742+
}
743+
744+
/**
745+
* @group legacy
746+
* @dataProvider getTestsForOctalNumbersYaml11Notation
747+
*/
748+
public function testParseOctalNumbersYaml11Notation(int $expected, string $yaml)
749+
{
750+
$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.');
751+
752+
self::assertSame($expected, Inline::parse($yaml));
753+
}
754+
755+
public function getTestsForOctalNumbersYaml11Notation()
737756
{
738757
return [
739758
'positive octal number' => [28, '034'],
759+
'positive octal number with separator' => [1243, '0_2_3_3_3'],
740760
'negative octal number' => [-28, '-034'],
741761
];
742762
}

0 commit comments

Comments
 (0)
0