-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS #22948
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
Conversation
@@ -490,11 +490,10 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar | |||
@trigger_error('Omitting the key of a mapping is deprecated and will throw a ParseException in 4.0.', E_USER_DEPRECATED); | |||
} | |||
|
|||
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags)) { | |||
if (!$isKeyQuoted) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added because quoted keys should not be evaluated
|
||
if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey)) { | ||
@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); | ||
if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey) && !is_int($evaluatedKey)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the is_int
check because unquoted integers work fine
I do not think this really is an option in general. The reason is that you may not be able to influence how the YAML file is actually dumped (e.g. an external data source) and not having quotes around mapping keys is totally valid. |
Using
Let's be pragmatic, I don't think someone will ever use
It shouldn't be for values that are not supported by php imo because you end up with a weird behavior for basically no gain (who would voluntarily ever use these values as keys?). |
307c083
to
c5210ad
Compare
Updated to target 3.4 |
Phpunit fails when using the 4.0 version of the Yaml component because of #22770, this exception must not be thrown when an integer is parsed (it is supported by php so there's no problem). |
@xabbuh indeed, pr rebased. The build still fails but this is not caused by this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh Is this one ok for you?
@@ -1843,27 +1818,13 @@ public function testPhpConstantTagMappingKey() | |||
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT)); | |||
} | |||
|
|||
public function testPhpConstantTagMappingKeyWithKeysCastToStrings() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, this test should not disappear. We need to keep it to ensure this case keeps working in 3.4 (it should become a legacy test though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember why i removed it so I guess we can revert this. However I won't be able to do it for the next 2 weeks, feel free to push on my repository if you have the time, otherwise I'll do the change ASAP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -236,9 +240,9 @@ private function doParse($value, $flags) | |||
throw $e; | |||
} | |||
|
|||
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags) && !is_string($key) && !is_int($key)) { | |||
if (!is_string($key) && !is_int($key)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure about this change ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, evaluable keys are no longer allowed unless they're quoted, even when using Yaml::PARSE_KEYS_AS_STRINGS
.
$keyType = is_numeric($key) ? 'numeric key' : 'non-string key'; | ||
@trigger_error(sprintf('Implicit casting of %s to string is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Pass the PARSE_KEYS_AS_STRINGS flag to explicitly enable the type casts.', $keyType), E_USER_DEPRECATED); | ||
@trigger_error(sprintf('Implicit casting of %s to string is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.', $keyType), E_USER_DEPRECATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest changing this message in 3.3 already. People who have not yet migrated to 3.3 should get the advice of quoting directly, to avoid a double migration for them. Quoting should already be working in 3.3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I'll submit a pr if this is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #23635
@@ -59,7 +59,7 @@ public function load($file, $type = null) | |||
} | |||
|
|||
try { | |||
$parsedConfig = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS); | |||
$parsedConfig = $this->yamlParser->parse(file_get_contents($path)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this a BC break ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not more than when we added it in 3.3 (for instance !str foo
in routing files was parsed as "foo"
in 3.2 and is as "!str foo"
since 3.3). Reverting this change is in fact more intuitive imo as it does make the parser always spec compliant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we do this change in #23635 then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer doing this in a minor but if you insist I can of course do it in 3.3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use
statement can be removed too?
Recommending to quote keys indeed looks like a good idea. However, I am not convinced we should deprecate the |
@xabbuh imo it causes too many issues for what it's worth. If the yaml file you try to parse uses features not supported by the sf parser nor by PHP, it looks normal and expectable to me that you can't parse this file. Lastly, I don't think this fits something else than very edge cases that could probably also be fixed by altering the yaml file parsed. |
200c9b9
to
21ada49
Compare
@@ -14,7 +14,6 @@ | |||
use Symfony\Component\Validator\Mapping\ClassMetadata; | |||
use Symfony\Component\Yaml\Exception\ParseException; | |||
use Symfony\Component\Yaml\Parser as YamlParser; | |||
use Symfony\Component\Yaml\Yaml; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this must be reverted
UPGRADE-3.4.md
Outdated
After: | ||
|
||
```php | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the blank line can be removed
UPGRADE-4.0.md
Outdated
After: | ||
|
||
```php | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be removed
@@ -59,7 +59,7 @@ public function load($file, $type = null) | |||
} | |||
|
|||
try { | |||
$parsedConfig = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS); | |||
$parsedConfig = $this->yamlParser->parse(file_get_contents($path)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we do this change in #23635 then?
/** | ||
* @dataProvider getNotPhpCompatibleMappingKeyData | ||
*/ | ||
public function testExplicitStringCastingOfMappingKeys($yaml, $expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the test should be kept but being flagged as legacy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -71,19 +71,6 @@ public function getDataFormSpecifications() | |||
} | |||
|
|||
/** | |||
* @dataProvider getNonStringMappingKeysData | |||
*/ | |||
public function testNonStringMappingKeys($expected, $yaml, $comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
@GuilhemN I would still expect floats to be used more frequently as keys than tags, but if noone else objects, I am fine with the proposed changes. I just left some minor comments. |
@xabbuh maybe but people just have to quote the key if they want to use a float while we have no solution for tags. And still the same argument, the yaml file is invalid and could be rejected by another parser (at least the other parser won't produce the same result) :) |
@@ -114,7 +114,7 @@ private function getClassesFromYaml() | |||
$this->yamlParser = new Parser(); | |||
} | |||
|
|||
$classes = $this->yamlParser->parse(file_get_contents($this->file), Yaml::PARSE_KEYS_AS_STRINGS); | |||
$classes = $this->yamlParser->parse(file_get_contents($this->file)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the use
statement be removed too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed
@xabbuh done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't the @deprecated
annotation be added to PARSE_KEYS_AS_STRINGS in Yaml.php?
@nicolas-grekas indeed. |
src/Symfony/Component/Yaml/Yaml.php
Outdated
@@ -31,6 +31,11 @@ class Yaml | |||
const PARSE_CONSTANT = 256; | |||
const PARSE_CUSTOM_TAGS = 512; | |||
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024; | |||
|
|||
/** | |||
* @deprecated since version 3.4, to be removed in 4.0. Quote your evaluable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be on one line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed :)
Thank you @GuilhemN. |
…TRINGS (GuilhemN) This PR was squashed before being merged into the 3.4 branch (closes #22948). Discussion ---------- [Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | yes <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | Sorry for opening this so lately... I just realized that we could get rid of `Yaml::PARSE_KEYS_AS_STRINGS` just by recommending using quotes... ~This way we don't allow a behavior not respecting the spec and we won't need to deprecate `PARSE_KEYS_AS_STRINGS` later.~ ~Is it too late for this to be merged in 3.3?~ ping @xabbuh Commits ------- b63c55c [Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
Sorry for opening this so lately... I just realized that we could get rid of
Yaml::PARSE_KEYS_AS_STRINGS
just by recommending using quotes...This way we don't allow a behavior not respecting the spec and we won't need to deprecatePARSE_KEYS_AS_STRINGS
later.Is it too late for this to be merged in 3.3?ping @xabbuh