Skip to content

Commit

Permalink
Protect against NPE with Option.IGNORE_IMPORTS
Browse files Browse the repository at this point in the history
Update `ConfigDataEnvironmentContributor` to deal with the fact that
the `properties` instance can be `null`.

Fixes gh-25029
  • Loading branch information
philwebb committed Jan 27, 2021
1 parent 7f32fa6 commit 0fcc52c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Expand Up @@ -223,7 +223,7 @@ public Iterator<ConfigDataEnvironmentContributor> iterator() {
ConfigDataEnvironmentContributor withBoundProperties(Binder binder) {
UseLegacyConfigProcessingException.throwIfRequested(binder);
ConfigDataProperties properties = ConfigDataProperties.get(binder);
if (this.configDataOptions.contains(ConfigData.Option.IGNORE_IMPORTS)) {
if (properties != null && this.configDataOptions.contains(ConfigData.Option.IGNORE_IMPORTS)) {
properties = properties.withoutImports();
}
return new ConfigDataEnvironmentContributor(Kind.BOUND_IMPORT, this.location, this.resource,
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Test;

import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.context.config.ConfigData.Option;
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.ImportPhase;
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind;
import org.springframework.boot.context.properties.bind.Binder;
Expand Down Expand Up @@ -334,6 +335,17 @@ void bindWhenHasUseLegacyPropertyThrowsException() {
() -> createBoundContributor(null, new ConfigData(Collections.singleton(propertySource)), 0));
}

@Test // gh-25029
void withBoundPropertiesWhenIgnoringImportsAndNothingBound() {
TestResource resource = new TestResource("a");
ConfigData configData = new ConfigData(Collections.singleton(new MockPropertySource()), Option.IGNORE_IMPORTS);
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofUnboundImport(TEST_LOCATION,
resource, false, configData, 0);
Binder binder = new Binder(contributor.getConfigurationPropertySource());
ConfigDataEnvironmentContributor bound = contributor.withBoundProperties(binder);
assertThat(bound).isNotNull();
}

private ConfigDataEnvironmentContributor createBoundContributor(String location) {
return createBoundContributor(new TestResource(location),
new ConfigData(Collections.singleton(new MockPropertySource())), 0);
Expand Down

0 comments on commit 0fcc52c

Please sign in to comment.