Builder defaults for collections are inconsistent with output record
Hi, awesome library! Love it! I just have a small suggestion...
If I have a record with any collection
@MyRecordBuilder // with useImmutableCollections = true, interpretNotNulls = true
public record Costs(
@Nullable BigDecimal shipping,
Map<String, Object> extraData
) { }
- the default values when the builder initializes for all fields is
null - calling
CostsBuilder.builder().build().extraData()builds instance ofCoststhat hasextraDatainitialized with emptyMap.of()and does not returnnull- which means
CostsBuilder.builder().build().extraData().get("value")will not throw NPE
- which means
- calling
CostsBuilder.builder().extraData()returns the defaultnull- which means
CostsBuilder.builder().extraData().get("value")will throw NPE
- which means
I'm proposing that since you're adding special handling of collections and always initializing them, they should also be initialized to empty collections when the empty builder is created, so that the second case will also not throw an NPE
Could these be combined into a single PR? Maybe with a new option to avoid breaking existing code.
@Randgalt I've created two issues because I expected others to have a different opinion and you might be willing to accept only one part of the proposal, but both of the issues are related in my mind.
Isn't this one solved by this?