Skip to content

Commit

Permalink
Add support for lists in SPRING_APPLICATION_JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer committed Oct 31, 2015
1 parent 986275c commit 5ed7156
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.env;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down Expand Up @@ -116,15 +117,28 @@ private void flatten(String prefix, Map<String, Object> result,
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,22 @@ public void prefixed() {
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:}"));
}

}

0 comments on commit 5ed7156

Please sign in to comment.