Add support for lists in SPRING_APPLICATION_JSON
This commit is contained in:
parent
986275c73d
commit
5ed7156061
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.env;
|
package org.springframework.boot.env;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
@ -116,16 +117,29 @@ public class SpringApplicationJsonEnvironmentPostProcessor
|
||||||
for (String key : map.keySet()) {
|
for (String key : map.keySet()) {
|
||||||
String name = prefix + key;
|
String name = prefix + key;
|
||||||
Object value = map.get(key);
|
Object value = map.get(key);
|
||||||
|
extract(name, result, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void extract(String name, Map<String, Object> result, Object value) {
|
||||||
if (value instanceof Map) {
|
if (value instanceof Map) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, Object> nested = (Map<String, Object>) value;
|
Map<String, Object> nested = (Map<String, Object>) value;
|
||||||
flatten(name, result, nested);
|
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 {
|
else {
|
||||||
result.put(name, value);
|
result.put(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private String findPropertySource(MutablePropertySources sources) {
|
private String findPropertySource(MutablePropertySources sources) {
|
||||||
if (ClassUtils.isPresent(
|
if (ClassUtils.isPresent(
|
||||||
|
|
|
||||||
|
|
@ -95,4 +95,22 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
|
||||||
assertEquals("spam", this.environment.resolvePlaceholders("${foo.bar:}"));
|
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…
Reference in New Issue