Skip to content

Commit

Permalink
Unescape the keys when config is returned as a map (#4678) (#4715)
Browse files Browse the repository at this point in the history
* Unescape the keys when config is returned as a map

* Update copyright year

* Using as(Map.class).get() and as(new GenericType<Map>(){}).get() should have keys unescaped
  • Loading branch information
klustria committed Aug 11, 2022
1 parent e0a28d9 commit b67c0eb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -631,11 +631,11 @@ public static Period toPeriod(String stringValue) {
*/
public static Map<String, String> toMap(Config config) {
if (config.isLeaf()) {
return new StringMap(config.key().toString(), config.asString().get());
return new StringMap(Config.Key.unescapeName(config.key().toString()), config.asString().get());
} else {
return new StringMap(config.traverse()
.filter(Config::isLeaf)
.map(node -> new AbstractMap.SimpleEntry<>(node.key().toString(), node.asString().get()))
.map(node -> new AbstractMap.SimpleEntry<>(Config.Key.unescapeName(node.key().toString()), node.asString().get()))
.collect(Collectors.toSet()));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -173,6 +173,11 @@ static ConfigValue<Map<String, String>> createMap(Config config,
Supplier<Optional<Map<String, String>>> valueSupplier = () -> {
Map<?, ?> map = mapperManager.map(config, Map.class);

map = map.entrySet().stream().collect(
Collectors.toMap(
entry -> Config.Key.unescapeName(entry.getKey().toString()), entry -> entry.getValue()
)
);
if (map instanceof ConfigMappers.StringMap) {
return Optional.of((ConfigMappers.StringMap) map);
}
Expand Down
44 changes: 33 additions & 11 deletions config/config/src/test/java/io/helidon/config/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package io.helidon.config;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -24,6 +25,7 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.helidon.common.GenericType;
import io.helidon.config.Config.Key;
import io.helidon.config.spi.ConfigNode.ListNode;
import io.helidon.config.spi.ConfigNode.ObjectNode;
Expand Down Expand Up @@ -528,6 +530,8 @@ public void testConfigKeyEscapedNameComplex() {
.addObject("oracle", ObjectNode.builder()
.addValue("com", "1")
.addValue("cz", "2")
// escaped key on a leaf node
.addValue(Key.escapeName("test.com"), "test")
.build())
.build()))
.disableEnvironmentVariablesSource()
Expand All @@ -542,6 +546,7 @@ public void testConfigKeyEscapedNameComplex() {
assertThat(config.get("oracle~1com.prop2").asString(), is(ConfigValues.simpleValue("val2")));
assertThat(config.get("oracle.com").asString(), is(ConfigValues.simpleValue("1")));
assertThat(config.get("oracle.cz").asString(), is(ConfigValues.simpleValue("2")));
assertThat(config.get("oracle.test~1com").asString(), is(ConfigValues.simpleValue("test")));

//name
assertThat(config.get("oracle~1com").name(), is("oracle.com"));
Expand All @@ -550,6 +555,7 @@ public void testConfigKeyEscapedNameComplex() {
assertThat(config.get("oracle").name(), is("oracle"));
assertThat(config.get("oracle.com").name(), is("com"));
assertThat(config.get("oracle.cz").name(), is("cz"));
assertThat(config.get("oracle.test~1com").name(), is("test.com"));

//child nodes
List<Config> children = config.asNodeList().get();
Expand All @@ -559,17 +565,33 @@ public void testConfigKeyEscapedNameComplex() {

//traverse
Set<String> keys = config.traverse().map(Config::key).map(Key::toString).collect(Collectors.toSet());
assertThat(keys, hasSize(6));
assertThat(keys, hasSize(7));
assertThat(keys, containsInAnyOrder("oracle~1com", "oracle~1com.prop1", "oracle~1com.prop2",
"oracle", "oracle.com", "oracle.cz"));

//map
Map<String, String> map = config.asMap().get();
assertThat(map.keySet(), hasSize(4));
assertThat(map.get("oracle~1com.prop1"), is("val1"));
assertThat(map.get("oracle~1com.prop2"), is("val2"));
assertThat(map.get("oracle.com"), is("1"));
assertThat(map.get("oracle.cz"), is("2"));
"oracle", "oracle.com", "oracle.cz", "oracle.test~1com"));

//return config as map using different methods and expect keys to be unescaped
for (Map<String, String> map : Arrays.asList(
config.asMap().get(),
config.as(Map.class).get(),
config.as(new GenericType<Map>(){}).get())
) {
assertThat(map.keySet(), hasSize(5));
assertThat(map.get("oracle.com.prop1"), is("val1"));
assertThat(map.get("oracle.com.prop2"), is("val2"));
assertThat(map.get("oracle.com"), is("1"));
assertThat(map.get("oracle.cz"), is("2"));
assertThat(map.get("oracle.test.com"), is("test"));
}
//escaped leaf node key returned as a map should expect key to be unescaped
String escapedLeafNode = "oracle.test~1com";
for (Map<String, String> map : Arrays.asList(
config.get(escapedLeafNode).asMap().get(),
config.get(escapedLeafNode).as(Map.class).get(),
config.get(escapedLeafNode).as(new GenericType<Map>(){}).get())
) {
assertThat(map.keySet(), hasSize(1));
assertThat(map.get("oracle.test.com"), is("test"));
}
}

@ExtendWith(RestoreSystemPropertiesExt.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ public void testConfigKeyEscapedNameComplex() {
assertThat(keys, containsInAnyOrder("oracle~1com", "oracle~1com.prop1", "oracle~1com.prop2",
"oracle", "oracle.com", "oracle.cz"));

//map
//map, expect keys to be unescaped
Map<String, String> map = config.asMap().get();
assertThat(map.keySet(), hasSize(4));
assertThat(map.get("oracle~1com.prop1"), is("val1"));
assertThat(map.get("oracle~1com.prop2"), is("val2"));
assertThat(map.get("oracle.com.prop1"), is("val1"));
assertThat(map.get("oracle.com.prop2"), is("val2"));
assertThat(map.get("oracle.com"), is("1"));
assertThat(map.get("oracle.cz"), is("2"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -565,8 +565,8 @@ public void testConfigKeyEscapedNameComplex() {
//map
Map<String, String> map = config.asMap().get();
assertThat(map.keySet(), hasSize(4));
assertThat(map.get("oracle~1com.prop1"), is("val1"));
assertThat(map.get("oracle~1com.prop2"), is("val2"));
assertThat(map.get("oracle.com.prop1"), is("val1"));
assertThat(map.get("oracle.com.prop2"), is("val2"));
assertThat(map.get("oracle.com"), is("1"));
assertThat(map.get("oracle.cz"), is("2"));
}
Expand Down

0 comments on commit b67c0eb

Please sign in to comment.