Skip to content

Commit 0fcc52c

Browse files
committed
Protect against NPE with Option.IGNORE_IMPORTS
Update `ConfigDataEnvironmentContributor` to deal with the fact that the `properties` instance can be `null`. Fixes gh-25029
1 parent 7f32fa6 commit 0fcc52c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public Iterator<ConfigDataEnvironmentContributor> iterator() {
223223
ConfigDataEnvironmentContributor withBoundProperties(Binder binder) {
224224
UseLegacyConfigProcessingException.throwIfRequested(binder);
225225
ConfigDataProperties properties = ConfigDataProperties.get(binder);
226-
if (this.configDataOptions.contains(ConfigData.Option.IGNORE_IMPORTS)) {
226+
if (properties != null && this.configDataOptions.contains(ConfigData.Option.IGNORE_IMPORTS)) {
227227
properties = properties.withoutImports();
228228
}
229229
return new ConfigDataEnvironmentContributor(Kind.BOUND_IMPORT, this.location, this.resource,

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.Test;
2727

2828
import org.springframework.boot.cloud.CloudPlatform;
29+
import org.springframework.boot.context.config.ConfigData.Option;
2930
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.ImportPhase;
3031
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind;
3132
import org.springframework.boot.context.properties.bind.Binder;
@@ -334,6 +335,17 @@ void bindWhenHasUseLegacyPropertyThrowsException() {
334335
() -> createBoundContributor(null, new ConfigData(Collections.singleton(propertySource)), 0));
335336
}
336337

338+
@Test // gh-25029
339+
void withBoundPropertiesWhenIgnoringImportsAndNothingBound() {
340+
TestResource resource = new TestResource("a");
341+
ConfigData configData = new ConfigData(Collections.singleton(new MockPropertySource()), Option.IGNORE_IMPORTS);
342+
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofUnboundImport(TEST_LOCATION,
343+
resource, false, configData, 0);
344+
Binder binder = new Binder(contributor.getConfigurationPropertySource());
345+
ConfigDataEnvironmentContributor bound = contributor.withBoundProperties(binder);
346+
assertThat(bound).isNotNull();
347+
}
348+
337349
private ConfigDataEnvironmentContributor createBoundContributor(String location) {
338350
return createBoundContributor(new TestResource(location),
339351
new ConfigData(Collections.singleton(new MockPropertySource())), 0);

0 commit comments

Comments
 (0)