java-client icon indicating copy to clipboard operation
java-client copied to clipboard

Ensure Builders are only called once and "lock" mutable builder parameters in object constructors.

Open bevzzz opened this issue 7 months ago • 0 comments

Builders in their current form suffer from a potential misuse where calling a builder setter after calling .build() may alter the state of a previously-create "target" object.

Consider:

var b = new CollectionConfig.Builder("CarelessCollection");
b.properties(Property.text("text_a"));
var cfg = b.build();

// Somewhere later in the code
b.properties(Property.text("text_b"));
client.collections.create(cfg);

CarelessCollection is created with 2 properties, despite what you might expect. Here's another good illustration of this point: https://github.com/weaviate/java-client/pull/399#discussion_r2179514203


We should:

  1. In all constructors that accept a Builder, wrap all mutable fields (list / map) in their unmodifiable views: Collections.unmodifiableList etc.
  2. Check .build() is only called once per object. Sth like this.

bevzzz avatar Jul 04 '25 12:07 bevzzz