Skip to content

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

Description

@bevzzz

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: #399 (comment)


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.

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions