Skip to content
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

[Yaml] deprecate implicit string casting of mapping keys #21774

Merged
merged 1 commit into from Mar 6, 2017

Conversation

Projects
None yet
7 participants
@xabbuh
Copy link
Member

commented Feb 27, 2017

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);

This comment has been minimized.

Copy link
@GuilhemN

GuilhemN Feb 27, 2017

Contributor

Shouldn't we also check that $evaluatedKey !== $key?

This comment has been minimized.

Copy link
@xabbuh

xabbuh Feb 27, 2017

Author Member

good idea

@@ -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;

This comment has been minimized.

Copy link
@GuilhemN

GuilhemN Feb 27, 2017

Contributor

It seems weird to me to allow something not allowed by the spec

This comment has been minimized.

Copy link
@xabbuh

xabbuh Feb 27, 2017

Author Member

If you just parse the YAML file and do not want to dump it afterwards (or consistency is not important), this may be enough.

This comment has been minimized.

Copy link
@GuilhemN

GuilhemN Feb 27, 2017

Contributor

Sure but it would work fine without it too so I don't think we should bother.

This comment has been minimized.

Copy link
@xabbuh

xabbuh Feb 27, 2017

Author Member

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.

This comment has been minimized.

Copy link
@GuilhemN

GuilhemN Feb 27, 2017

Contributor

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.

This comment has been minimized.

Copy link
@xabbuh

xabbuh Feb 27, 2017

Author Member

Why would it be invalid?

This comment has been minimized.

Copy link
@GuilhemN

GuilhemN Feb 27, 2017

Contributor

Because it does not respect the spec

This comment has been minimized.

Copy link
@stof

stof Feb 27, 2017

Member

@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).

This comment has been minimized.

Copy link
@GuilhemN

GuilhemN Feb 27, 2017

Contributor

Sure but then do you think we should allow someone to use false: foo while it's not possible in php ?

@xabbuh xabbuh force-pushed the xabbuh:deprecate-not-usable-key-parsing branch from ce0b647 to c82b566 Feb 27, 2017

if (!(Yaml::PARSE_KEYS_AS_STRING & $flags)) {
$evaluatedKey = self::evaluateScalar($key, $flags, $references);
if ($evaluatedKey !== $key && !is_string($evaluatedKey) && !is_int($evaluatedKey)) {

This comment has been minimized.

Copy link
@GuilhemN

GuilhemN Feb 27, 2017

Contributor

!is_int($evaluated) should be removed imo, it is not supported currently so it should trigger a deprecation too.

@xabbuh xabbuh force-pushed the xabbuh:deprecate-not-usable-key-parsing branch from c82b566 to fa23697 Feb 27, 2017

@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Feb 27, 2017

@GuilhemN

This comment has been minimized.

Copy link
Contributor

commented Feb 27, 2017

What about block mappings?

@xabbuh xabbuh force-pushed the xabbuh:deprecate-not-usable-key-parsing branch 3 times, most recently from 81c4c79 to 0206282 Mar 6, 2017

@xabbuh

This comment has been minimized.

Copy link
Member Author

commented Mar 6, 2017

tests for block mappings added


$expected = array(
1 => 'foo',
0 => 'bar',

This comment has been minimized.

Copy link
@xabbuh

xabbuh Mar 6, 2017

Author Member

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).

@fabpot

This comment has been minimized.

Copy link
Member

commented Mar 6, 2017

👍 (with a note added in the CHANGELOG)

@xabbuh xabbuh force-pushed the xabbuh:deprecate-not-usable-key-parsing branch from 0206282 to 3c5d915 Mar 6, 2017

@xabbuh

This comment has been minimized.

Copy link
Member Author

commented Mar 6, 2017

Changelog and upgrade files updated, I also renamed PARSE_KEYS_AS_STRING to PARSE_KEYS_AS_STRINGS for consistency.

@fabpot

This comment has been minimized.

Copy link
Member

commented Mar 6, 2017

Thank you @xabbuh.

@fabpot fabpot merged commit 3c5d915 into symfony:master Mar 6, 2017

2 of 3 checks passed

fabbot.io Some changes should be done to comply with our standards.
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

fabpot added a commit that referenced this pull request Mar 6, 2017

feature #21774 [Yaml] deprecate implicit string casting of mapping ke…
…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

@xabbuh xabbuh deleted the xabbuh:deprecate-not-usable-key-parsing branch Mar 6, 2017

@fabpot fabpot referenced this pull request May 1, 2017

Merged

Release v3.3.0-BETA1 #22603

lcobucci added a commit to lcobucci/doctrine2 that referenced this pull request May 30, 2017

Force YAML parser to convert keys to string
Since Symfony 3.3 implicit conversion is not enabled by default so we
need to pass that flag manually.

Related to: symfony/symfony#21774

lcobucci added a commit to lcobucci/doctrine2 that referenced this pull request May 30, 2017

Force YAML parser to convert keys to string
Since Symfony 3.3 implicit conversion is not enabled by default so we
need to pass that flag manually.

Related to: symfony/symfony#21774
@plachance

This comment has been minimized.

Copy link

commented Jun 8, 2017

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.

@xabbuh

This comment has been minimized.

Copy link
Member Author

commented Jun 13, 2017

@plachance Can you please open a new issue with an example YAML snippet that is causing this deprecation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.