spring-projects / spring-projects/spring-boot
Support null values when binding properties
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 81.5k
- Forks
- 42.7k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 65
Description
Application has a ben annotated with@ConfigurationProperties("test") that has a List<String> attribute called users. at the beginning, one item listed in the external configuration file
test:
users:
- Andy
as the application is running, change the configuration file to the following
test:
users:
or
test:
users: null
what I expect is users=null or users =[], while the result is still users[0]=Andy
it‘s ok for the following pattern :
test:
users: ""
Here's a test that fails with Spring Boot 2.1.17.RELEASE
@SpringBootTest(classes=ConfigPropertiesTest.class)
@RunWith(SpringRunner.class)
@EnableConfigurationProperties(ConfigPropertiesTest.TestProperties.class)
@TestPropertySource(properties = {"test.users[0]=Andy"})
public class ConfigPropertiesTest {
@Autowired
private TestProperties properties;
@Autowired
private ConfigurationPropertiesBindingPostProcessor processor;
@Autowired
private ConfigurableEnvironment environment;
@Test
public void liveRebindToNull() throws Exception {
assertEquals(properties.getUsers().get(0),"Andy");
setNullProperties("test.users");
assertTrue(environment.getPropertySources().get("test").containsProperty("test.users"));;
assertNull(environment.getPropertySources().get("test").getProperty("test.users"));
processor.postProcessBeforeInitialization(properties, "testProperties");
assertEquals(properties.getUsers().size(), 0); //**_TEST NG_**
}
/**
* TestPropertyValues has no public method set null value, using reflect to do it
*
* @param prefix
* @throws Exception
*/
private void setNullProperties(String prefix) throws Exception {
TestPropertyValues testPropertyValues = TestPropertyValues.empty();
Method method = TestPropertyValues.class.getDeclaredMethod("and", Stream.class);
method.setAccessible(true);
TestPropertyValues.Pair pair = new TestPropertyValues.Pair(prefix,null);
testPropertyValues = (TestPropertyValues) method.invoke(testPropertyValues, Stream.of(pair));
testPropertyValues.applyTo(environment);
}
@ConfigurationProperties("test")
public static class TestProperties {
private List<String> users;
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> users) {
this.users = users;
}
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the failing liveRebindToNull test and ConfigurationPropertiesBindingPostProcessor in the provided reproduction. Trace how a null test.users property is handled during rebinding, then add coverage for the null and empty YAML cases; done means the bound users list no longer retains Andy.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100