1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge pull request #1167 from yuhuangbin/nacos-loaddata

[Bugfix] Resolve the type erase caused by loading Nacos data
This commit is contained in:
Jim Fang 2020-01-15 15:16:15 +08:00 committed by GitHub
commit 23c3dd69ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 71 deletions

View File

@ -17,7 +17,6 @@
package com.alibaba.cloud.nacos.client;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@ -111,17 +110,7 @@ public class NacosPropertySourceBuilder {
@SuppressWarnings("unchecked")
private Map<String, Object> propertiesToMap(Properties properties) {
Map<String, Object> result = new HashMap<>(16);
Enumeration<String> keys = (Enumeration<String>) properties.propertyNames();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = properties.getProperty(key);
if (value != null) {
result.put(key, value.trim());
}
else {
result.put(key, null);
}
}
properties.forEach((k, v) -> result.put(String.valueOf(k), v));
return result;
}

View File

@ -111,12 +111,12 @@ public abstract class AbstractNacosDataParser {
/**
* Generate key-value pairs from the map.
*/
protected Properties generateProperties(Map<String, String> map) {
protected Properties generateProperties(Map<String, Object> map) {
if (null == map || map.isEmpty()) {
return null;
}
Properties properties = new Properties();
for (Map.Entry<String, String> entry : map.entrySet()) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (StringUtils.isEmpty(key)) {
continue;
@ -130,12 +130,12 @@ public abstract class AbstractNacosDataParser {
/**
* Reload the key ending in `value` if need.
*/
protected Map<String, String> reloadMap(Map<String, String> map) {
protected Map<String, Object> reloadMap(Map<String, Object> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> result = new HashMap<>(map);
for (Map.Entry<String, String> entry : map.entrySet()) {
Map<String, Object> result = new HashMap<>(map);
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (key.contains(DOT)) {
int idx = key.lastIndexOf(DOT);

View File

@ -17,18 +17,22 @@
package com.alibaba.cloud.nacos.parser;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* @author zkz
* @author yuhuangbin
*/
public class NacosDataJsonParser extends AbstractNacosDataParser {
@ -41,7 +45,7 @@ public class NacosDataJsonParser extends AbstractNacosDataParser {
if (StringUtils.isEmpty(data)) {
return null;
}
Map<String, String> map = parseJSON2Map(data);
Map<String, Object> map = parseJSON2Map(data);
return this.generateProperties(this.reloadMap(map));
}
@ -51,44 +55,48 @@ public class NacosDataJsonParser extends AbstractNacosDataParser {
* @return the map convert by json string
* @throws IOException thrown if there is a problem parsing config.
*/
public static Map<String, String> parseJSON2Map(String json) throws IOException {
Map<String, String> map = new HashMap<>(32);
public static Map<String, Object> parseJSON2Map(String json) throws IOException {
Map<String, Object> result = new HashMap<>(32);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
if (null == jsonNode) {
return map;
Map<String, Object> nacosDataMap = mapper.readValue(json, Map.class);
if (CollectionUtils.isEmpty(nacosDataMap)) {
return result;
}
parseJsonNode(map, jsonNode, "");
return map;
parseNacosDataMap(result, nacosDataMap, "");
return result;
}
private static void parseJsonNode(Map<String, String> jsonMap, JsonNode jsonNode,
String parentKey) {
Iterator<String> fieldNames = jsonNode.fieldNames();
while (fieldNames.hasNext()) {
String name = fieldNames.next();
String fullKey = StringUtils.isEmpty(parentKey) ? name
: parentKey + DOT + name;
JsonNode resultValue = jsonNode.findValue(name);
if (null == resultValue) {
private static void parseNacosDataMap(Map<String, Object> result,
Map<String, Object> dataMap, String parentKey) {
Set<Map.Entry<String, Object>> entries = dataMap.entrySet();
for (Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); iterator
.hasNext();) {
Map.Entry<String, Object> entry = iterator.next();
String key = entry.getKey();
Object value = entry.getValue();
String fullKey = StringUtils.isEmpty(parentKey) ? key : key.startsWith("[")
? parentKey.concat(key) : parentKey.concat(DOT).concat(key);
if (value instanceof Map) {
Map<String, Object> map = (Map<String, Object>) value;
parseNacosDataMap(result, map, fullKey);
continue;
}
if (resultValue.isArray()) {
Iterator<JsonNode> iterator = resultValue.elements();
while (iterator != null && iterator.hasNext()) {
JsonNode next = iterator.next();
if (null == next) {
continue;
}
parseJsonNode(jsonMap, next, fullKey);
else if (value instanceof Collection) {
int count = 0;
Collection<Object> collection = (Collection<Object>) value;
for (Object object : collection) {
parseNacosDataMap(result,
Collections.singletonMap("[" + (count++) + "]", object),
fullKey);
}
continue;
}
if (resultValue.isObject()) {
parseJsonNode(jsonMap, resultValue, fullKey);
continue;
}
jsonMap.put(fullKey, resultValue.asText());
result.put(fullKey, value);
}
}

View File

@ -50,13 +50,13 @@ public class NacosDataXmlParser extends AbstractNacosDataParser {
if (StringUtils.isEmpty(data)) {
return null;
}
Map<String, String> map = parseXml2Map(data);
Map<String, Object> map = parseXml2Map(data);
return this.generateProperties(this.reloadMap(map));
}
private Map<String, String> parseXml2Map(String xml) throws IOException {
private Map<String, Object> parseXml2Map(String xml) throws IOException {
xml = xml.replaceAll("\\r", "").replaceAll("\\n", "").replaceAll("\\t", "");
Map<String, String> map = new HashMap<>(32);
Map<String, Object> map = new HashMap<>(32);
try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
@ -73,7 +73,7 @@ public class NacosDataXmlParser extends AbstractNacosDataParser {
return map;
}
private void parseNodeList(NodeList nodeList, Map<String, String> map,
private void parseNodeList(NodeList nodeList, Map<String, Object> map,
String parentKey) {
if (nodeList == null || nodeList.getLength() < 1) {
return;
@ -104,7 +104,7 @@ public class NacosDataXmlParser extends AbstractNacosDataParser {
}
}
private void parseNodeAttr(NamedNodeMap nodeMap, Map<String, String> map,
private void parseNodeAttr(NamedNodeMap nodeMap, Map<String, Object> map,
String parentKey) {
if (null == nodeMap || nodeMap.getLength() < 1) {
return;

View File

@ -39,6 +39,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@ -51,8 +52,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class,
properties = { "spring.application.name=xmlApp", "spring.profiles.active=dev",
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, properties = {
"spring.application.name=xmlApp", "spring.profiles.active=dev",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.config.namespace=test-namespace",
"spring.cloud.nacos.config.encode=utf-8",
@ -65,10 +66,9 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.config.ext-config[0].data-id=ext-json-test.json",
"spring.cloud.nacos.config.ext-config[1].data-id=ext-common02.properties",
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
"spring.cloud.nacos.config.shared-dataids=shared-data1.properties",
"spring.cloud.nacos.config.shared-dataids=shared-data1.properties,shared-data.json",
"spring.cloud.nacos.config.accessKey=test-accessKey",
"spring.cloud.nacos.config.secretKey=test-secretKey" },
webEnvironment = NONE)
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
public class NacosConfigurationXmlJsonTest {
static {
@ -136,6 +136,22 @@ public class NacosConfigurationXmlJsonTest {
return "shared-name=shared-value-1";
}
if ("shared-data.json".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "{\n" + " \"test\" : {\n"
+ " \"name\" : \"test\",\n"
+ " \"list\" : [\n" + " {\n"
+ " \"name\" :\"listname1\",\n"
+ " \"age\":1\n" + " },\n"
+ " {\n"
+ " \"name\" :\"listname2\",\n"
+ " \"age\":2\n" + " }\n"
+ " ],\n" + " \"metadata\" : {\n"
+ " \"intKey\" : 123,\n"
+ " \"booleanKey\" : true\n" + " }\n"
+ " }\n" + "}";
}
return "";
}
});
@ -156,6 +172,9 @@ public class NacosConfigurationXmlJsonTest {
@Autowired
private NacosRefreshHistory refreshHistory;
@Autowired
private Environment environment;
@Test
public void contextLoads() throws Exception {
@ -176,6 +195,27 @@ public class NacosConfigurationXmlJsonTest {
checkoutEndpoint();
checkJsonParser();
}
private void checkJsonParser() {
assertThat(environment.getProperty("test.name", String.class)).isEqualTo("test");
assertThat(environment.getProperty("test.list[0].name", String.class))
.isEqualTo("listname1");
assertThat(environment.getProperty("test.list[0].age", Integer.class))
.isEqualTo(1);
assertThat(environment.getProperty("test.list[1].name", String.class))
.isEqualTo("listname2");
assertThat(environment.getProperty("test.list[1].age", Integer.class))
.isEqualTo(2);
assertThat(
(Integer) environment.getProperty("test.metadata.intKey", Object.class))
.isEqualTo(123);
assertThat((Boolean) environment.getProperty("test.metadata.booleanKey",
Object.class)).isEqualTo(true);
}
private void checkoutNacosConfigServerAddr() {