Supports dotted keys in properties-to-yaml conversion.

Fixes gh-1510
This commit is contained in:
Iskra Delta
2019-11-22 20:39:19 +01:00
committed by Spencer Gibb
parent b2508eb97c
commit 0d100a7080
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;
@@ -324,12 +323,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"));
}
/**
@@ -352,6 +346,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;
@@ -362,19 +359,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<>();
@@ -383,7 +388,8 @@ public class EnvironmentController {
setListValue(list, value);
}
else {
map.put(key, value);
// use compound prefix
map.put(prefix + key, value);
}
}

View File

@@ -298,6 +298,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));
when(this.repository.findOne("ay", "äzöq", null, false))
.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>();
@@ -341,7 +355,7 @@ public class EnvironmentControllerTests {
" - d: z\n" +
" - - r\n" +
" - s\n";
// @formatter:on
// @formatter:on
assertThat(yaml).as("Wrong output: " + yaml).isEqualTo(expected);
}