From 754c7477cdc1ea5d46c664c8afa71136d40ba8a0 Mon Sep 17 00:00:00 2001 From: sakanaou Date: Thu, 8 Dec 2016 20:43:23 +0100 Subject: [PATCH] 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. --- .../cloud/consul/config/ConsulPropertySource.java | 4 ++++ .../consul/config/ConsulPropertySourceTests.java | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java index 9730085b..2053ffe4 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java @@ -119,6 +119,10 @@ public class ConsulPropertySource extends EnumerablePropertySource 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()) { diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java index 0f7a8f62..6bb278ae 100644 --- a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceTests.java @@ -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();