Skip to content

Add Yaml::PARSE_EXCEPTION_ON_DUPLICATE to throw exceptions on duplicates #19529

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

Closed
wants to merge 13 commits into from

Conversation

alexpott
Copy link
Contributor

@alexpott alexpott commented Aug 4, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #19526
License MIT
Doc PR

@linaori
Copy link
Contributor

linaori commented Aug 4, 2016

I'd love this feature. In a few big applications with 25+ developers, mistakes like this happen every once in a while. I love to see them crash instead of silently continuing.

@GuilhemN
Copy link
Contributor

GuilhemN commented Aug 4, 2016

As proposed by other people, i would prefer deprecate this and throw an exception in symfony 4.0.

@nicolas-grekas
Copy link
Member

@alexpott OK to replace this flag by a deprecation?

@@ -475,6 +475,8 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
// are processed sequentially.
if (!isset($output[$key])) {
$output[$key] = $value;
} else {
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.3 and will cause an exception in 4.0.', $key), E_USER_DEPRECATED);
Copy link
Contributor

@ogizanagi ogizanagi Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be since 3.2, right ?

Also should be:

- and will cause an exception
+ and will throw an exception

Copy link
Contributor

@ogizanagi ogizanagi Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also indicates the thrown exception will be an instance of Symfony\Component\Yaml\Exception\ParseException

@alexpott
Copy link
Contributor Author

alexpott commented Aug 8, 2016

The failure in fabbot.io is on a line not changed by this patch - I guess src/Symfony/Component/Yaml/Tests/Fixtures/YtsSpecificationExamples.yml does not comply with the standard.

@fabpot
Copy link
Member

fabpot commented Aug 8, 2016

👍

@@ -771,7 +771,7 @@ public function testScalarInSequence()
* @see http://yaml.org/spec/1.2/spec.html#id2759572
* @see http://yaml.org/spec/1.1/#id932806
*/
public function testMappingDuplicateKeyBlock()
public function testLegacyMappingDuplicateKeyBlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing @group legacy annotation (and no need to rename the test case then)
same below

@xabbuh
Copy link
Member

xabbuh commented Aug 8, 2016

Please also update the upgrade files for 3.2 and 4.0.

@@ -230,6 +230,10 @@ Yaml
* The `!!php/object` tag to indicate dumped PHP objects was removed in favor of
the `!php/object` tag.

* Support for silently ignoring duplicate keys in YAML has been deprecated and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 4.0 it will be longer deprecated.

@fabpot
Copy link
Member

fabpot commented Aug 9, 2016

Thank you @alexpott.

@fabpot fabpot closed this Aug 9, 2016
fabpot added a commit that referenced this pull request Aug 9, 2016
…ions on duplicates (Alex Pott)

This PR was squashed before being merged into the 3.2-dev branch (closes #19529).

Discussion
----------

Add Yaml::PARSE_EXCEPTION_ON_DUPLICATE to throw exceptions on duplicates

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19526
| License       | MIT
| Doc PR        |

Commits
-------

cb362f2 Add Yaml::PARSE_EXCEPTION_ON_DUPLICATE to throw exceptions on duplicates
@stof
Copy link
Member

stof commented Sep 7, 2016

IIRC, the Yaml spec explicitly says that parsers should use the first key when finding duplicates (and we made a bugfix in the past to respect it instead of using the last one). So this deprecation is a violation of the spec

@GuilhemN
Copy link
Contributor

GuilhemN commented Sep 7, 2016

@stof from what i see, the spec says that it should be considered as an error:

If both notations are used as keys in the same mapping, only a YAML processor which recognizes integer formats would correctly flag the duplicate key as an error.

And:

Technically, YAML therefore complies with the JSON spec, choosing to treat duplicates as an error. In practice, since JSON is silent on the semantics of such duplicates, the only portable JSON files are those with unique keys, which are therefore valid YAML files.

@fabpot fabpot mentioned this pull request Oct 27, 2016
@ivangretsky
Copy link

Good day!
Was looking for exact same feature. It is said to be in the master, but I cannot find it. Am I missing something?

@xabbuh
Copy link
Member

xabbuh commented Jul 28, 2017

@ivangretsky There is no flag controlling this. The parser just triggers a deprecation when detecting duplicated keys on 3.2 or higher and will throw an exception starting with Symfony 4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.