@ConfigurationProperties with initialized nested record properties values no longer bind #34407
Labels
type: regression
A regression from a previous release
Milestone
We are in the process of migrating to Spring Boot 3.0 and we noticed that nested record properties with default values can no longer be bound.
Given a configuration class...
... and the
NestedProperty
, which is a record:Test (using Spock):
The test runs successfully using Spring Boot 2.7.8, but fails on 3.0.2. The reason is this if-statement that was introduced by @philwebb with this commit to fix gh-33409. The idea for this change was to force setter binding instead of constructor binding for JavaBeans if the target value already exists (by throwing away the "deduced bind constructor"), to match the JavaBeans binding behavior in Spring 2.x.
Unfortunately, this change does not really make sense for records, as records are immutable and hence setter binding cannot be used. I would expect Spring 3.x to distinguish between the two use-cases (setter binding for JavaBeans vs. constructor binding for records) just like Spring 2.x did.
Workaround until this is fixed
As a workaround, using constructor binding for records can be enforced using
@ConstructorBinding
on a (theoretically redundant) additional constructor:Now, the binding constructor is not considered "deduced" any more and is used as expected.
The text was updated successfully, but these errors were encountered: