8000 feature #34813 [Yaml] support YAML 1.2 octal notation, deprecate YAML… · symfony/symfony@09d78cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 09d78cf

Browse files
committed
feature #34813 [Yaml] support YAML 1.2 octal notation, deprecate YAML 1.1 one (xabbuh)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Yaml] support YAML 1.2 octal notation, deprecate YAML 1.1 one | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | Fix #34807 | License | MIT | Doc PR | Commits ------- 440e0b7 support YAML 1.2 octal notation, deprecate YAML 1.1 one
2 parents 0d4bba8 + 440e0b7 commit 09d78cf

File tree

7 files changed

+90
-12
lines changed

7 files changed

+90
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,14 @@ 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+
$value = str_replace('_', '', $matches['value']);
636+
637+
if ('-' === $scalar[0]) {
638+
return -octdec($value);
639+
} else {
640+
return octdec($value);
641+
}
634642

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

655+
if ('0' === $scalar[0] && '0' !== $scalar) {
656+
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.');
657+
}
658+
647659
return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
648660
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
649661
$raw = $scalar;
650662
$cast = (int) $scalar;
651663

664+
if ('0' === $scalar[1] && '-0' !== $scalar) {
665+
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.');
666+
}
667+
652668
return '0' == $scalar[1] ? -octdec(substr($scalar, 1)) : (($raw === (string) $cast) ? $cast : $raw);
653669
case is_numeric($scalar):
654670
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: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ 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],
303+
['0o_2_3_3_3', 02333],
304304
['.Inf', -log(0)],
305305
['-.Inf', log(0)],
306306
["'686e444'", '686e444'],
@@ -379,7 +379,7 @@ public function getTestsForParseWithMapObjects()
379379
["'quoted string'", 'quoted string'],
380380
['12.30e+02', 12.30e+02],
381381
['0x4D2', 0x4D2],
382-
['02333', 02333],
382+
['0o2333', 02333],
383383
['.Inf', -log(0)],
384384
['-.Inf', log(0)],
385385
["'686e444'", '686e444'],
@@ -734,9 +734,30 @@ public function testParseOctalNumbers($expected, $yaml)
734734
}
735735

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

0 commit comments

Comments
 (0)
0