Merge branch '2.1.x'

This commit is contained in:
Spencer Gibb
2019-12-16 14:23:24 -05:00
2 changed files with 33 additions and 13 deletions

View File

@@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -353,12 +352,7 @@ public class EnvironmentController {
}
private void postProcessProperties(Map<String, Object> propertiesMap) {
for (Iterator<String> iter = propertiesMap.keySet().iterator(); iter.hasNext();) {
String key = iter.next();
if (key.equals("spring.profiles")) {
iter.remove();
}
}
propertiesMap.keySet().removeIf(key -> key.equals("spring.profiles"));
}
/**
@@ -381,6 +375,9 @@ public class EnvironmentController {
private final String propertyKey;
// Supports keys like org.x and org.x.y like in boot logging
private String prefix = "";
private int currentPos;
private NodeType valueType;
@@ -391,19 +388,27 @@ public class EnvironmentController {
this.valueType = NodeType.MAP;
}
@SuppressWarnings("unchecked")
private void setMapValue(Map<String, Object> map, Object value) {
String key = getKey();
if (NodeType.MAP.equals(this.valueType)) {
@SuppressWarnings("unchecked")
Map<String, Object> nestedMap = (Map<String, Object>) map.get(key);
if (nestedMap == null) {
Map<String, Object> nestedMap;
if (map.get(key) instanceof Map) {
nestedMap = (Map<String, Object>) map.get(key);
}
else if (map.get(key) != null) {
// not an object, set prefix for later
prefix = key + ".";
nestedMap = map;
}
else {
// value of key is null
nestedMap = new LinkedHashMap<>();
map.put(key, nestedMap);
}
setMapValue(nestedMap, value);
}
else if (NodeType.ARRAY.equals(this.valueType)) {
@SuppressWarnings("unchecked")
List<Object> list = (List<Object>) map.get(key);
if (list == null) {
list = new ArrayList<>();
@@ -412,7 +417,8 @@ public class EnvironmentController {
setListValue(list, value);
}
else {
map.put(key, value);
// use compound prefix
map.put(prefix + key, value);
}
}

View File

@@ -302,6 +302,20 @@ public class EnvironmentControllerTests {
assertThat(yaml).isEqualTo("- a: c\n- a: d\n");
}
@Test
public void yamlWithProperties() throws Exception {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("org.springframework", "WARN");
map.put("org.springframework.cloud", "ERROR");
this.environment.add(new PropertySource("abo", map));
Mockito.when(this.repository.findOne("ay", "äzöq", null))
.thenReturn(this.environment);
System.out.println("this.controller = " + this.controller);
String yaml = this.controller.yaml("ay", "äzöq", false).getBody();
assertThat(yaml).isEqualTo(
"org:\n springframework: WARN\n springframework.cloud: ERROR\n");
}
@Test
public void arrayOfObjectInYaml() throws Exception {
Map<String, Object> map = new LinkedHashMap<String, Object>();
@@ -345,7 +359,7 @@ public class EnvironmentControllerTests {
" - d: z\n" +
" - - r\n" +
" - s\n";
// @formatter:on
// @formatter:on
assertThat(yaml).as("Wrong output: " + yaml).isEqualTo(expected);
}