Add support for lists in SPRING_APPLICATION_JSON

pull/4359/head
Dave Syer 9 years ago
parent 986275c73d
commit 5ed7156061

@ -16,6 +16,7 @@
package org.springframework.boot.env;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
@ -116,15 +117,28 @@ public class SpringApplicationJsonEnvironmentPostProcessor
for (String key : map.keySet()) {
String name = prefix + key;
Object value = map.get(key);
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> nested = (Map<String, Object>) value;
flatten(name, result, nested);
}
else {
result.put(name, value);
extract(name, result, value);
}
}
private void extract(String name, Map<String, Object> result, Object value) {
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> nested = (Map<String, Object>) value;
flatten(name, result, nested);
}
if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> nested = (Collection<Object>) value;
int index = 0;
for (Object object : nested) {
extract(name + "[" + index + "]", result, object);
index++;
}
}
else {
result.put(name, value);
}
}
private String findPropertySource(MutablePropertySources sources) {

@ -95,4 +95,22 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
assertEquals("spam", this.environment.resolvePlaceholders("${foo.bar:}"));
}
@Test
public void list() {
assertEquals("", this.environment.resolvePlaceholders("${foo[1]:}"));
EnvironmentTestUtils.addEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[\"bar\",\"spam\"]}");
this.processor.postProcessEnvironment(this.environment, null);
assertEquals("spam", this.environment.resolvePlaceholders("${foo[1]:}"));
}
@Test
public void listOfObject() {
assertEquals("", this.environment.resolvePlaceholders("${foo[0].bar:}"));
EnvironmentTestUtils.addEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[{\"bar\":\"spam\"}]}");
this.processor.postProcessEnvironment(this.environment, null);
assertEquals("spam", this.environment.resolvePlaceholders("${foo[0].bar:}"));
}
}

Loading…
Cancel
Save