cuizhennan / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
1 stars 0 forks source link

SnakeYaml does not flag duplicate keys #199

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The Yaml spec 1.1 dictates that keys are unique (e.g. sections 3.1.1 and 3.2.1)

However, using "Instant Yaml", for example, the YAML file:

foo:
   bar: 1
   bar: 2

results in 

%YAML 1.1

---
!!map {
    ? !!str "foo"
    : !!map {
        ? !!str "bar"
        : !!int "2",
    },
}
...

with no indication of the duplicate keys.

I suggest a DumperOption method setUniqueKeysRequired(boolean), with default 
"false" so as to not break existing applications.

If a duplicate key is found during scanning, with setUniqueKeysRequired(true), 
we would throw a MarkedYamlException indicating the error.

Original issue reported on code.google.com by MarkL...@gmail.com on 22 Sep 2014 at 1:03

GoogleCodeExporter commented 9 years ago
Well, the spec references and proposals (for DumperOption) are all about 
serialization, while the example is about DE-serialization.
Let us clearly split it to avoid confusion.
Can you please provide some complete Java code which you expect to work ?
(I cannot think about a data structure in Java with non-unique keys)

Please consider the following:
- how other parsers (non Java) behave
- what spec 1.2 says in this case (it is more relaxed, because of JSON 
compatibility)
- if you start a conversation in the main YAML list and get a consensus there - 
it would be a perfect solution.

Original comment by py4fun@gmail.com on 22 Sep 2014 at 8:51

GoogleCodeExporter commented 9 years ago
Thanks for your comments. I agree that DumperOption is not the best place for 
this function. 

Also, I couldn't see how to register this item as an improvement vs. bug.

I will open up a thread on the main YAML list, and I've started on some JAVA 
code.

Original comment by MarkL...@gmail.com on 22 Sep 2014 at 1:20

GoogleCodeExporter commented 9 years ago
For your information, there is this issue in the Spring Boot project that seems 
related: https://github.com/spring-projects/spring-boot/issues/1683

Original comment by stephane...@gmail.com on 14 Oct 2014 at 1:34

GoogleCodeExporter commented 9 years ago
I am one of the Spring Boot leads. I don't think Spring Boot users are 
interested in dumper options. We have a perfectly good workaround (a custom 
Constructor), so no need to make changes if you don't want to, but it does seem 
strange to me that SnakeYAML would silently bind to a Map and swallow the 
duplicate keys with not even a warning (we prefer an exception since if the 
user is binding to a Map, the duplicate keys are probably an error).

Original comment by david.s...@googlemail.com on 16 Oct 2014 at 10:32

GoogleCodeExporter commented 9 years ago
I agree completely - my original proposal was not the best approach. The custom 
Constructor is a much better idea.

Operationally, should I close the original item (#199) and open a new item? I 
couldn't see how to revise the original proposal. Sorry about my unfamiliarity 
with the tracking website functions.

Original comment by MarkL...@gmail.com on 16 Oct 2014 at 6:16

GoogleCodeExporter commented 9 years ago
Please check the change: 
https://code.google.com/p/snakeyaml/source/detail?r=58ea501dd9599d5d527664771d9f
080cfe878f9b

This is backwards incompatible.
Let us see how we can make it configurable.

Original comment by py4fun@gmail.com on 4 Feb 2015 at 9:36