I have this simple configuration:
@ConfigurationProperties("dummy")
public record TestRecord(@DefaultValue Set<String> values, @DefaultValue("default") String one) {
}
This fails with : Parameter of type java.util.Set<java.lang.String> must have a non-empty default value.
What I would like to get as a default value is an empty Set. Of course as a workaround, I can:
public Set<String> values() {
return values == null ? Set.of() : values;
}
but considering there are any of these default collections/maps, I do not want to add an accessor for each of the properties. Is there are a way to make @DefaultValue "return" an empty Set? thank you.
Comment From: philwebb
@wind57 This looks like a duplicate of #18917 which should have been fixed in Spring Boot 2.2.7. Which version of Spring Boot are you using? Are you able to provide a sample application that shows the problem?
Comment From: wind57
@ConfigurationProperties("dummy")
public record TestRecord(@DefaultValue Set<String> values, @DefaultValue("default") String one) {
}
and a test:
@Test
void testSpringBindingFields() {
new ApplicationContextRunner().withUserConfiguration(UnderTest.Config.class)
.run(context -> {
TestRecord props = context.getBean(TestRecord.class);
assertThat(props).isNotNull();
assertThat(props.one()).isEqualTo("default");
assertThat(props.values().size()).isEqualTo(0);
});
}
@Configuration
@EnableConfigurationProperties(TestRecord.class)
static class Config {
}
This is for 3.0.0-M5. I am actually trying to simplify some code in spring-cloud-kubernetes , this were I spotted it.
thank you for looking into it.
Comment From: philwebb
Thanks, I misread our current code and thought that it dealt with collection types, but it doesn't. I think we should change that and allow @DefaultValue to mean an empty instance.
Comment From: wind57
That was fast! Thank you.
Comment From: wind57
would you happen to know when this one will reach spring-boot-3? just want to put a note for myself to slightly change some code when that happens, so that I do not forget; nothing more. thank you
Comment From: snicoll
@wind57 all fixes are merged forward at the same time as the fix itself. Please follow the linked issues for more details, that is https://github.com/spring-projects/spring-boot/issues/32564
Comment From: wind57
thank you.