-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] deprecate implicit string casting of mapping keys #21774
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
xabbuh
commented
Feb 27, 2017
•
edited
Loading
edited
Q | A |
---|---|
Branch? | master |
Bug fix? | no |
New feature? | no |
BC breaks? | no |
Deprecations? | yes |
Tests pass? | yes |
Fixed tickets | #21650 (comment) |
License | MIT |
Doc PR |
@@ -485,6 +485,14 @@ 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_STRING & $flags)) { | |||
$evaluatedKey = self::evaluateScalar($key, $flags, $references); |
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 also check that $evaluatedKey !== $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.
good idea
src/Symfony/Component/Yaml/Yaml.php
Outdated
@@ -30,6 +30,7 @@ class Yaml | |||
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128; | |||
const PARSE_CONSTANT = 256; | |||
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024; | |||
const PARSE_KEYS_AS_STRING = 2048; |
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.
It seems weird to me to allow something not allowed by the spec
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.
If you just parse the YAML file and do not want to dump it afterwards (or consistency is not important), this may be enough.
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.
Sure but it would work fine without it too so I don't think we should bother.
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.
But sometimes you are not able to control how the actual YAML structure looks like. I don't think we should make it harder to parse such files.
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.
But then the file is invalid, so shouldn't we consider it must be updated?
If someone has the case why not but it seems a bit too early imo to add this without being sure it will be used.
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.
Why would it be invalid?
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.
Because it does not respect the spec
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.
@GuilhemN the YAML file is not invalid. It is incompatible with PHP datastructures. YAML allows any value as key of a mapping (including floats, boolean, sequences and mapping).
PHP only supports strings and integers (and it accepts floats but casts them as integer, which is loosing data).
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.
Sure but then do you think we should allow someone to use false: foo
while it's not possible in php ?
ce0b647
to
c82b566
Compare
if (!(Yaml::PARSE_KEYS_AS_STRING & $flags)) { | ||
$evaluatedKey = self::evaluateScalar($key, $flags, $references); | ||
|
||
if ($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.
!is_int($evaluated)
should be removed imo, it is not supported currently so it should trigger a deprecation too.
c82b566
to
fa23697
Compare
What about block mappings? |
81c4c79
to
0206282
Compare
tests for block mappings added |
|
||
$expected = array( | ||
1 => 'foo', | ||
0 => 'bar', |
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.
That's actually an inconsistency in the current behaviour of the parser for inlined and block mapping (the keys would be parsed as the strings true
and false
inside an inlined mapping).
👍 (with a note added in the CHANGELOG) |
0206282
to
3c5d915
Compare
Changelog and upgrade files updated, I also renamed |
Thank you @xabbuh. |
…ys (xabbuh) This PR was merged into the 3.3-dev branch. Discussion ---------- [Yaml] deprecate implicit string casting of mapping keys | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #21650 (comment) | License | MIT | Doc PR | Commits ------- 3c5d915 deprecate implicit string casting of mapping keys
Since Symfony 3.3 implicit conversion is not enabled by default so we need to pass that flag manually. Related to: symfony/symfony#21774
Since Symfony 3.3 implicit conversion is not enabled by default so we need to pass that flag manually. Related to: symfony/symfony#21774
The YAML linter throws the error "Implicit casting of incompatible key type to string on line X is deprecated since version 3.3" when we use !php/const in keys even if the value of the constant is a string. Is it the intended behavior? I don't fully understand the YAML specification on that regard. |
@plachance Can you please open a new issue with an example YAML snippet that is causing this deprecation? |