Allow multi-level immutable references

Issue #348 resolved
Muhammet YILDIZ created an issue

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)

  1. Alexander Maslov

    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.

  2. Log in to comment