Return empty array if ConfigurationPropertySource.from returns null

Fixes gh-1062
This commit is contained in:
spencergibb
2022-01-31 20:51:55 -05:00
parent 1ce1201e9d
commit 05382d3053
2 changed files with 27 additions and 3 deletions

View File

@@ -53,6 +53,8 @@ public abstract class ConfigDataMissingEnvironmentPostProcessor implements Envir
private static final Bindable<String[]> CONFIG_DATA_LOCATION_ARRAY = Bindable.of(String[].class);
private static final String[] EMPTY_ARRAY = new String[0];
/**
* Order of post processor, set to run after
* {@link ConfigDataEnvironmentPostProcessor}.
@@ -110,15 +112,19 @@ public abstract class ConfigDataMissingEnvironmentPostProcessor implements Envir
}
private String[] getConfigImportArray(PropertySource propertySource) {
Binder binder = new Binder(ConfigurationPropertySource.from(propertySource));
ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource);
if (configurationPropertySource == null) {
return EMPTY_ARRAY;
}
Binder binder = new Binder(configurationPropertySource);
return binder.bind(CONFIG_IMPORT_PROPERTY, CONFIG_DATA_LOCATION_ARRAY, new BindHandler() {
@Override
public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context,
Exception error) throws Exception {
LOG.info("Error binding " + CONFIG_IMPORT_PROPERTY, error);
return new String[0];
return EMPTY_ARRAY;
}
}).orElse(new String[0]);
}).orElse(EMPTY_ARRAY);
}
public static class ImportException extends RuntimeException {

View File

@@ -17,13 +17,18 @@
package org.springframework.cloud.commons;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.env.MockPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
@@ -31,6 +36,7 @@ import static org.mockito.Mockito.mock;
/**
* @author Ryan Baxter
*/
@ExtendWith(OutputCaptureExtension.class)
public class ConfigDataMissingEnvironmentPostProcessorTests {
@Test
@@ -92,6 +98,18 @@ public class ConfigDataMissingEnvironmentPostProcessorTests {
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importHandlesNullConfigurationPropertySource(CapturedOutput output) {
MockEnvironment environment = new MockEnvironment();
ConfigurationPropertySources.attach(environment);
environment.setProperty("spring.config.import[0]", "configserver:http://localhost:8888");
environment.setProperty("spring.config.import[1]", "file:./app.properties");
SpringApplication app = mock(SpringApplication.class);
TestConfigDataMissingEnvironmentPostProcessor processor = new TestConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
assertThat(output).doesNotContain("Error binding spring.config.import");
}
public class TestConfigDataMissingEnvironmentPostProcessor extends ConfigDataMissingEnvironmentPostProcessor {
@Override