- changed status to open
Dumping Enum breaks in 1.19 - 1.21
First, thanks for a great product with snakeyaml.
While upgrading snakeyaml 1.10 to 1.21 I noticed an backward incompatibility with how snakeyaml dumps Java enums starting in 1.19. I've included a test case below that shows what I expect to happen. Essentially I would expect the enum to be dumped as just the name of the enum, just as it is in 1.10 through 1.18. Instead the enum is dumped as !!YamlTest$MyEnum$2 'B'
, which causes a YAMLException when trying to load it.
I am using Java 8.
public class YamlTest {
public static class Bean {
public MyEnum myEnum = MyEnum.B;
}
public enum MyEnum {
A {}, B {};
}
/**
* Test dumping enums. Works on Yaml 1.10 through 1.18. Fails on Yaml 1.19 through
* 1.21.
*/
@Test
public void testYaml() {
Yaml yaml = new Yaml();
String text = yaml.dumpAs(new Bean(), Tag.MAP, FlowStyle.AUTO);
Bean actual = yaml.loadAs(text, Bean.class);
assertThat(actual.myEnum, is(MyEnum.B));
assertThat(text, is("{myEnum: B}\n"));
}
}
Comments (11)
-
-
reporter Hi Andrey, thanks for the response. I reduced the code to the minimum amount needed to reproduce the bug. In Java each enum value can be it its own class which makes them much more powerful than in other languages. The practice is recommended by Joshua Bloch in "Effective Java" when all the instances of a certain type can be enumerated at compile time.
To give a real world example, I noticed the issue when I was working with https://www.orekit.org/static/xref/org/orekit/frames/LOFType.html
-
I see the point now. The next 2 weeks we will not be able to work on this issue. Hopefully, we can fix it and release with the next version at the end of August.
-
reporter Thanks! that would be great!
-
Dear Evan, I have added a commit with more context: https://bitbucket.org/asomov/snakeyaml/commits/cdeb4e36339f0ee7bc456abeee8cd7d1191dbe1c
Feel free to study it. You can see why the change was done (to support generics). You can also see which test is affected.
Proposals are welcome !
Anonymous inner class gets $2 at the end and it breaks both dumping and loading.
-
- changed status to resolved
Fix issue 409: check only property.getType()
Refactor the way to detect if the full tag can be omitted (and !!str is used instead). Instead of checking whether the class for property and the class for the actual value is the same we can just check that the property class is not java.lang.Enum
→ <<cset 93a3f65e578b>>
-
Feel free to try the latest SNAPSHOT
-
- changed status to open
It was auto closed
-
reporter I've tested your changes with my code base and it fixes the issue. Thanks!
-
Can you survive with the SNAPSHOT ?
It looks like this is the only change from the last release.
-
reporter I'll continue using 1.18 until the fix is released since I don't need any features from 1.19+. I'll update when 1.22 is released. Thanks for implementing the fix quickly.
- Log in to comment
Dear Evan, I have added your test:
https://bitbucket.org/asomov/snakeyaml/src/default/src/test/java/org/yaml/snakeyaml/issues/issue409/DumpEnumAsJavabeanPropertyTest.java
You use a special way to define enum. Please check my comment.