Avoid NPE if YAML/PROPERTIES data value is empty (#257)

Can happen if empty file content is sent to Consul's K/V store
or data is manually edited via the UI.
This commit is contained in:
sakanaou
2016-12-08 20:43:23 +01:00
committed by Spencer Gibb
parent 595951c30c
commit 754c7477cd
2 changed files with 17 additions and 0 deletions

View File

@@ -119,6 +119,10 @@ public class ConsulPropertySource extends EnumerablePropertySource<ConsulClient>
protected void parseValue(GetValue getValue, ConsulConfigProperties.Format format) {
String value = getValue.getDecodedValue();
if (value == null) {
return;
}
Properties props = generateProperties(value, format);
for (Map.Entry entry : props.entrySet()) {

View File

@@ -94,6 +94,19 @@ public class ConsulPropertySourceTests {
assertProperties(source, "fooymlval", 8080);
}
@Test
public void testEmptyYaml() {
// yaml file property
String yamlContext = prefix + "/yaml";
client.setKVValue(yamlContext+"/data", "");
ConsulConfigProperties configProperties = new ConsulConfigProperties();
configProperties.setFormat(ConsulConfigProperties.Format.YAML);
ConsulPropertySource source = new ConsulPropertySource(yamlContext, client, configProperties);
// Should NOT through a NPE
source.init();
}
private ConsulPropertySource getConsulPropertySource(ConsulConfigProperties configProperties, String context) {
ConsulPropertySource source = new ConsulPropertySource(context, client, configProperties);
source.init();