Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty @DefaultValue cannot be used on collections, maps, arrays or optional values #32559

Closed
wind57 opened this issue Sep 30, 2022 · 7 comments
Closed
Labels
type: bug A general bug
Milestone

Comments

@wind57
Copy link

wind57 commented Sep 30, 2022

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 30, 2022
@philwebb
Copy link
Member

@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?

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Sep 30, 2022
@wind57
Copy link
Author

wind57 commented Sep 30, 2022

@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.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 30, 2022
@philwebb
Copy link
Member

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.

@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Sep 30, 2022
@philwebb philwebb modified the milestones: 2.6.13, 2.6.x Sep 30, 2022
@philwebb philwebb changed the title @DefaultValue empty Collections/Maps Empty @DefaultValue cannot be used on collections, maps, arrays or optional values Sep 30, 2022
@philwebb philwebb modified the milestones: 2.6.x, 2.6.13 Oct 1, 2022
@wind57
Copy link
Author

wind57 commented Oct 1, 2022

That was fast! Thank you.

@wind57
Copy link
Author

wind57 commented Oct 8, 2022

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

@snicoll
Copy link
Member

snicoll commented Oct 9, 2022

@wind57 all fixes are merged forward at the same time as the fix itself. Please follow the linked issues for more details, that is #32564

@wind57
Copy link
Author

wind57 commented Oct 9, 2022

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants