8000 deprecate implicit string casting of mapping keys · symfony/symfony@81c4c79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81c4c79

Browse files
committed
deprecate implicit string casting of mapping keys
1 parent e7c12d3 commit 81c4c79

21 files changed

+264
-101
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ protected function loadFile($file)
602602
}
603603

604604
try {
605-
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
605+
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_KEYS_AS_STRING);
606606
} catch (ParseException $e) {
607607
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
608608
}

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Yaml\Exception\ParseException;
1818
use Symfony\Component\Yaml\Parser as YamlParser;
1919
use Symfony\Component\Config\Loader\FileLoader;
20+
use Symfony\Component\Yaml\Yaml;
2021

2122
/**
2223
* YamlFileLoader loads Yaml routing files.
@@ -58,7 +59,7 @@ public function load($file, $type = null)
5859
}
5960

6061
try {
61-
$parsedConfig = $this->yamlParser->parse(file_get_contents($path));
62+
$parsedConfig = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRING);
6263
} catch (ParseException $e) {
6364
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
6465
}

src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1616
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
1717
use Symfony\Component\Yaml\Parser;
18+
use Symfony\Component\Yaml\Yaml;
1819

1920
/**
2021
* YAML File Loader.
@@ -113,7 +114,7 @@ private function getClassesFromYaml()
113114
$this->yamlParser = new Parser();
114115
}
115116

116-
$classes = $this->yamlParser->parse(file_get_contents($this->file));
117+
$classes = $this->yamlParser->parse(file_get_contents($this->file), Yaml::PARSE_KEYS_AS_STRING);
117118

118119
if (empty($classes)) {
119120
return array();

src/Symfony/Component/Translation/Loader/YamlFileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Translation\Exception\LogicException;
1616
use Symfony\Component\Yaml\Parser as YamlParser;
1717
use Symfony\Component\Yaml\Exception\ParseException;
18+
use Symfony\Component\Yaml\Yaml;
1819

1920
/**
2021
* YamlFileLoader loads translations from Yaml files.
@@ -39,7 +40,7 @@ protected function loadResource($resource)
3940
}
4041

4142
try {
42-
$messages = $this->yamlParser->parse(file_get_contents($resource));
43+
$messages = $this->yamlParser->parse(file_get_contents($resource), Yaml::PARSE_KEYS_AS_STRING);
4344
} catch (ParseException $e) {
4445
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
4546
}

src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Mapping\ClassMetadata;
1515
use Symfony\Component\Yaml\Exception\ParseException;
1616
use Symfony\Component\Yaml\Parser as YamlParser;
17+
use Symfony\Component\Yaml\Yaml;
1718

1819
/**
1920
* Loads validation metadata from a YAML file.
@@ -115,7 +116,7 @@ protected function parseNodes(array $nodes)
115116
private function parseFile($path)
116117
{
117118
try {
118-
$classes = $this->yamlParser->parse(file_get_contents($path));
119+
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRING);
119120
} catch (ParseException $e) {
120121
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
121122
}

src/Symfony/Component/Yaml/Inline.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
485485
@trigger_error('Omitting the key of a mapping is deprecated and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
486486
}
487487

488+
if (!(Yaml::PARSE_KEYS_AS_STRING & $flags)) {
489+
$evaluatedKey = self::evaluateScalar($key, $flags, $references);
490+
491+
if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey)) {
492+
@trigger_error('Implicit casting of incompatible mapping keys to strings is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Pass the PARSE_KEYS_AS_STRING flag to explicitly enable the type casts.', E_USER_DEPRECATED);
493+
}
494+
}
495+
488496
if (':' !== $key && (!isset($mapping[$i + 1]) || !in_array($mapping[$i + 1], array(' ', ',', '[', ']', '{', '}'), true))) {
489497
@trigger_error('Using a colon that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}" is deprecated since version 3.2 and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
490498
}

src/Symfony/Component/Yaml/Parser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,19 @@ public function parse($value, $flags = 0)
180180
Inline::parse(null, $flags, $this->refs);
181181
try {
182182
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
183-
$key = Inline::parseScalar($values['key']);
183+
$i = 0;
184+
$key = Inline::parseScalar($values['key'], 0, null, $i, !(Yaml::PARSE_KEYS_AS_STRING & $flags));
184185
} catch (ParseException $e) {
185186
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
186187
$e->setSnippet($this->currentLine);
187188

188189
throw $e;
189190
}
190191

192+
if (!(Yaml::PARSE_KEYS_AS_STRING & $flags) && !is_string($key)) {
193+
@trigger_error('Implicit casting of incompatible mapping keys to strings is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Pass the PARSE_KEYS_AS_STRING flag to explicitly enable the type casts.', E_USER_DEPRECATED);
194+
}
195+
191196
// Convert float keys to strings, to avoid being converted to integers by PHP
192197
if (is_float($key)) {
193198
$key = (string) $key;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testSpecifications()
125125
// TODO
126126
} else {
127127
eval('$expected = '.trim($test['php']).';');
128-
$this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
128+
$this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10), Yaml::PARSE_KEYS_AS_STRING), $test['test']);
129129
}
130130
}
131131
}

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -556,21 +556,6 @@ php: |
556556
'fixed' => 1230.15,
557557
)
558558
---
559-
test: Miscellaneous
560-
spec: 2.21
561-
yaml: |
562-
null: ~
563-
true: true
564-
false: false
565-
string: '12345'
566-
php: |
567-
array(
568-
'' => null,
569-
1 => true,
570-
0 => false,
571-
'string' => '12345'
572-
)
573-
---
574559
test: Timestamps
575560
todo: true
576561
spec: 2.22
@@ -1533,18 +1518,6 @@ ruby: |
15331518
}
15341519
15351520
---
1536-
test: Boolean
1537-
yaml: |
1538-
false: used as key
1539-
logical: true
1540-
answer: false
1541-
php: |
1542-
array(
1543-
false => 'used as key',
1544-
'logical' => true,
1545-
'answer' => false
1546-
)
1547-
---
15481521
test: Integer
15491522
yaml: |
15501523
canonical: 12345

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,6 @@ php: |
210210
'negative one-thousand' => -1000.0
211211
)
212212
---
213-
test: Integers as Map Keys
214-
brief: >
215-
An integer can be used a dictionary key.
216-
yaml: |
217-
1: one
218-
2: two
219-
3: three
220-
php: |
221-
array(
222-
1 => 'one',
223-
2 => 'two',
224-
3 => 'three'
225-
)
226-
---
227213
test: Floats
228214
dump_skip: true
229215
brief: >
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- %YAML:1.0
2+
test: Miscellaneous
3+
spec: 2.21
4+
yaml: |
5+
true: true
6+
false: false
7+
php: |
8+
array(
9+
'true' => true,
10+
'false' => false,
11+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--- %YAML:1.0
2+
test: Miscellaneous
3+
spec: 2.21
4+
yaml: |
5+
true: true
6+
false: false
7+
php: |
8+
array(
9+
1 => true,
10+
0 => false,
11+
)
12+
---
13+
test: Boolean
14+
yaml: |
15+
false: used as key
16+
logical: true
17+
answer: false
18+
php: |
19+
array(
20+
false => 'used as key',
21+
'logical' => true,
22+
'answer' => false
23+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- legacyBooleanMappingKeys
2+
- legacyNullMappingKey
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- %YAML:1.0
2+
test: Miscellaneous
3+
spec: 2.21
4+
yaml: |
5+
null: ~
6+
php: |
7+
array(
8+
'' => null,
9+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- booleanMappingKeys
2+
- numericMappingKeys
3+
- nullMappingKey
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- %YAML:1.0
2+
test: Miscellaneous
3+
spec: 2.21
4+
yaml: |
5+
null: ~
6+
php: |
7+
array(
8+
'null' => null,
9+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--- %YAML:1.0
2+
test: A sequence with an unordered array
3+
brief: >
4+
A sequence with an unordered array
5+
yaml: |
6+
1: foo
7+
0: bar
8+
php: |
9+
array(1 => 'foo', 0 => 'bar')
10+
---
11+
test: Integers as Map Keys
12+
brief: >
13+
An integer can be used as dictionary key.
14+
yaml: |
15+
1: one
16+
2: two
17+
3: three
18+
php: |
19+
array(
20+
1 => 'one',
21+
2 => 'two',
22+
3 => 'three'
23+
)

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@ yaml: |
9696
php: |
9797
array('foo', array('bar' => array('bar' => 'foo')))
9898
---
99-
test: A sequence with an unordered array
100-
brief: >
101-
A sequence with an unordered array
102-
yaml: |
103-
1: foo
104-
0: bar
105-
php: |
106-
array(1 => 'foo', 0 => 'bar')
107-
---
10899
test: Octal
109100
brief: as in spec example 2.19, octal value is converted
110101
yaml: |

0 commit comments

Comments
 (0)
0