Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| } | ||
| 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 theis_int check because unquoted integers work fine
xabbuh commentedMay 30, 2017
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. |
GuilhemN commentedMay 30, 2017
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 toc5210adCompareGuilhemN commentedMay 30, 2017
Updated to target 3.4 |
GuilhemN commentedJun 7, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 commentedJun 13, 2017
GuilhemN commentedJun 13, 2017
@xabbuh indeed, pr rebased. The build still fails but this is not caused by this PR. |
fabpot left a 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.
@xabbuh Is this one ok for you?
| $this->assertSame($expected,$this->parser->parse($yaml, Yaml::PARSE_CONSTANT)); | ||
| } | ||
| publicfunctiontestPhpConstantTagMappingKeyWithKeysCastToStrings() |
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
| } | ||
| 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 usingYaml::PARSE_KEYS_AS_STRINGS.
| if (!is_string($key) && !is_int($key)) { | ||
| $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
| 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.
Theuse statement can be removed too?
xabbuh commentedJul 19, 2017
Recommending to quote keys indeed looks like a good idea. However, I am not convinced we should deprecate the |
GuilhemN commentedJul 19, 2017
@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 to21ada49Compare| useSymfony\Component\Validator\Mapping\ClassMetadata; | ||
| useSymfony\Component\Yaml\Exception\ParseException; | ||
| useSymfony\Component\Yaml\ParserasYamlParser; | ||
| useSymfony\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
| 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
| 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 | ||
| */ | ||
| publicfunctiontestExplicitStringCastingOfMappingKeys($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
| /** | ||
| * @dataProvider getNonStringMappingKeysData | ||
| */ |
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
xabbuh commentedJul 24, 2017
@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. |
GuilhemN commentedJul 24, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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) :) |
| } | ||
| $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 theuse 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 commentedJul 26, 2017
GuilhemN commentedJul 26, 2017
@xabbuh done |
nicolas-grekas left a 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.
shouldn't the@deprecated annotation be added to PARSE_KEYS_AS_STRINGS in Yaml.php?
GuilhemN commentedAug 1, 2017
@nicolas-grekas indeed. |
src/Symfony/Component/Yaml/Yaml.php Outdated
| constDUMP_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 :)
xabbuh commentedAug 3, 2017
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@xabbuhCommits-------b63c55c [Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
Uh oh!
There was an error while loading.Please reload this page.
Sorry for opening this so lately... I just realized that we could get rid of
Yaml::PARSE_KEYS_AS_STRINGSjust 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_STRINGSlater.Is it too late for this to be merged in 3.3?ping@xabbuh