FreeBuilder icon indicating copy to clipboard operation
FreeBuilder copied to clipboard

Nested buildable types should use the enclosing type's defaults

Open alicederyn opened this issue 4 years ago • 0 comments

Default values set on a nested buildable type in the constructor should be ignored when merging, but unfortunately they are currently treated as explicitly-set values, as the merge method in the nested mergeFrom method uses the defaults from its own constructor.

e.g.

@FreeBuilder
interface TypeA {
  int value();  // No default
  class Builder extends TypeA_Builder { }
}

@FreeBuilder
interface TypeB {
  TypeA a();  // Value defaults to 0 on this type
  class Builder extends TypeB_Builder {
    Builder() {
      super().mutateA(a -> a.value(0));
    }
  }
}

TypeB.Builder x = new TypeB.Builder();
TypeB.Builder b = new TypeB.Builder();
b.a().value(10);
b.mergeFrom(x);  // This should not overwrite a.value
y = b.build();
assertEquals(10, y.a()); // Fails

Not sure how to easily fix this — would probably require an API extension, to have the option of passing a different default into the mergeFrom algorithm. It would also break existing code, so probably needs to be a major version bump when fixed.

alicederyn avatar Nov 09 '20 21:11 alicederyn