Allow multi-level immutable references
When immutable objects are created with as following:
public class Foo {
public Foo() {}
public Foo(String str) { }
}
public class Bar {
public Bar(){ }
public Bar(Foo foo){ }
}
public class Baz {
public Baz() { }
public Baz(Bar bar) { }
}
foo:
- &foo !!model.Foo ["foo"]
bar:
- &bar !!model.Bar [*foo]
baz:
- &baz !!model.Baz [*bar]
The library produces the following error:
Caused by: org.yaml.snakeyaml.error.YAMLException: Immutable objects cannot be recursive.
at org.yaml.snakeyaml.constructor.Constructor$ConstructSequence.construct2ndStep(Constructor.java:653)
Reader position:
in 'reader', line 4, column 5:
- &bar !!model.Bar [*foo]
In other words, since all the objects are immutable and I reference *bar for creating &baz, I receive this error.
As a hack to this, I do the following.
public class Foo {
public Foo() {}
public Foo(String str) { }
}
public class Bar {
private Foo foo;
public Bar(){ }
public Bar(Foo[] foo){ this.foo = foo[0]; }
}
public class Baz {
public Baz() { }
public Baz(Bar bar) { }
}
And convert the first reference to a sequence
foo:
- &foo !!model.Foo ["foo"]
bar:
- &bar !!model.Bar [[*foo]]
baz:
- &baz !!model.Baz [*bar]
and it works.
However, I believe it should allow the normal case without having to refer to objects in arrays/sequences
(I use snakeyaml 1.7)
Comments (8)
-
-
reporter I added a maven based sample here:
https://bitbucket.org/myildiz83/snakeyamimmutable.git
under src/test/java/model/ you should see SnakeYamlImmutableTest.java. There are two tests there, the first one is the failing, the second one is with the workaround.
Kind Regards
-
- changed status to open
Thank you. I can reproduce the issue.
-
it is actually nothing to do with ImmutableObjects. This bug is about not removing parent object when composed object is an anchor.
I will push a fix.
-
- changed status to resolved
fixes
#348. Parent node is removed from recursive nodes also in case of Alias(anchor) node composition (wasn't like this before).→ <<cset ed8907a29237>>
-
- changed status to open
It was auto-closed
-
- changed status to resolved
Sorry...
-
It will be released in 1.18 Expected in Feb 2017
- Log in to comment
I do not see any recursion here. Can you extract a test ?