diff --git a/config/config/src/main/java/io/helidon/config/ConfigMappers.java b/config/config/src/main/java/io/helidon/config/ConfigMappers.java index 52a0c72a134..eba5a4ec568 100644 --- a/config/config/src/main/java/io/helidon/config/ConfigMappers.java +++ b/config/config/src/main/java/io/helidon/config/ConfigMappers.java @@ -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. @@ -631,11 +631,11 @@ public static Period toPeriod(String stringValue) { */ public static Map 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())); } } diff --git a/config/config/src/main/java/io/helidon/config/ConfigValues.java b/config/config/src/main/java/io/helidon/config/ConfigValues.java index 3f83f72d06b..3418da3415f 100644 --- a/config/config/src/main/java/io/helidon/config/ConfigValues.java +++ b/config/config/src/main/java/io/helidon/config/ConfigValues.java @@ -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. @@ -173,6 +173,11 @@ static ConfigValue> createMap(Config config, Supplier>> 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); } diff --git a/config/config/src/test/java/io/helidon/config/ConfigTest.java b/config/config/src/test/java/io/helidon/config/ConfigTest.java index 47a6ff9949d..9317dc5c11c 100644 --- a/config/config/src/test/java/io/helidon/config/ConfigTest.java +++ b/config/config/src/test/java/io/helidon/config/ConfigTest.java @@ -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. @@ -16,6 +16,7 @@ package io.helidon.config; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; @@ -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; @@ -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() @@ -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")); @@ -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 children = config.asNodeList().get(); @@ -559,17 +565,33 @@ public void testConfigKeyEscapedNameComplex() { //traverse Set 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 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 map : Arrays.asList( + config.asMap().get(), + config.as(Map.class).get(), + config.as(new GenericType(){}).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 map : Arrays.asList( + config.get(escapedLeafNode).asMap().get(), + config.get(escapedLeafNode).as(Map.class).get(), + config.get(escapedLeafNode).as(new GenericType(){}).get()) + ) { + assertThat(map.keySet(), hasSize(1)); + assertThat(map.get("oracle.test.com"), is("test")); + } } @ExtendWith(RestoreSystemPropertiesExt.class) diff --git a/config/hocon/src/test/java/io/helidon/config/hocon/HoconConfigParserTest.java b/config/hocon/src/test/java/io/helidon/config/hocon/HoconConfigParserTest.java index 715f6f2592a..670193c8356 100644 --- a/config/hocon/src/test/java/io/helidon/config/hocon/HoconConfigParserTest.java +++ b/config/hocon/src/test/java/io/helidon/config/hocon/HoconConfigParserTest.java @@ -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 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")); } diff --git a/config/tests/integration-tests/src/test/java/io/helidon/config/tests/AbstractComplexConfigTest.java b/config/tests/integration-tests/src/test/java/io/helidon/config/tests/AbstractComplexConfigTest.java index 0fdce925201..99ae33b4eba 100644 --- a/config/tests/integration-tests/src/test/java/io/helidon/config/tests/AbstractComplexConfigTest.java +++ b/config/tests/integration-tests/src/test/java/io/helidon/config/tests/AbstractComplexConfigTest.java @@ -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. @@ -565,8 +565,8 @@ public void testConfigKeyEscapedNameComplex() { //map Map 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")); }