From b3eb149152e718428a790aeeb6ef59ccdf2e07dd Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Wed, 22 Jul 2020 13:57:04 +0100 Subject: [PATCH] Hide indexed propertyNames from YAML source. (#360) --- sources/yaml/pom.xml | 5 ++ .../config/source/yaml/YamlConfigSource.java | 29 +++++++-- .../config/source/yaml/BasicTest.java | 27 ++------ .../source/yaml/YamlConfigSourceTest.java | 62 +++++++++++++++++++ sources/yaml/src/test/resources/example.yml | 22 +++++++ 5 files changed, 118 insertions(+), 27 deletions(-) create mode 100644 sources/yaml/src/test/resources/example.yml diff --git a/sources/yaml/pom.xml b/sources/yaml/pom.xml index 2a59be693..0f2445f67 100644 --- a/sources/yaml/pom.xml +++ b/sources/yaml/pom.xml @@ -43,6 +43,11 @@ smallrye-config test + + jakarta.annotation + jakarta.annotation-api + test + diff --git a/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java b/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java index afab4d0a0..a678f0b49 100644 --- a/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java +++ b/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java @@ -6,7 +6,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.TreeMap; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.eclipse.microprofile.config.spi.ConfigSource; @@ -28,20 +30,32 @@ public class YamlConfigSource extends MapBackedConfigSource { private static final long serialVersionUID = -418186029484956531L; + private final Set propertyNames; + + public YamlConfigSource(String name, Map source, int ordinal) { + super(name, source, ordinal, false); + this.propertyNames = filterIndexedNames(source.keySet()); + } + public YamlConfigSource(String name, InputStream stream) throws IOException { this(name, stream, ORDINAL); } public YamlConfigSource(String name, InputStream stream, int defaultOrdinal) throws IOException { - super(name, streamToMap(stream), defaultOrdinal, false); + this(name, streamToMap(stream), defaultOrdinal); } - public YamlConfigSource(String name, String str) { - this(name, str, ORDINAL); + public YamlConfigSource(String name, String source) { + this(name, source, ORDINAL); } - public YamlConfigSource(String name, String str, int defaultOrdinal) { - super(name, stringToMap(str), defaultOrdinal, false); + public YamlConfigSource(String name, String source, int ordinal) { + this(name, stringToMap(source), ordinal); + } + + @Override + public Set getPropertyNames() { + return propertyNames; } @SuppressWarnings("unchecked") @@ -136,4 +150,9 @@ private static void escapeCommas(StringBuilder b, String src, int escapeLevel) { b.appendCodePoint(cp); } } + + private static Set filterIndexedNames(Set names) { + final Pattern pattern = Pattern.compile(".*\\[[0-9]+].*"); + return names.stream().filter(s -> !pattern.matcher(s).find()).collect(Collectors.toSet()); + } } diff --git a/sources/yaml/src/test/java/io/smallrye/config/source/yaml/BasicTest.java b/sources/yaml/src/test/java/io/smallrye/config/source/yaml/BasicTest.java index d9239dfe3..80e8de246 100644 --- a/sources/yaml/src/test/java/io/smallrye/config/source/yaml/BasicTest.java +++ b/sources/yaml/src/test/java/io/smallrye/config/source/yaml/BasicTest.java @@ -4,13 +4,11 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import org.eclipse.microprofile.config.spi.ConfigSource; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -public class BasicTest { - +class BasicTest { @Test - public void testBasicKeyValue() { + void basicKeyValue() { String yaml = "foo:\n" + " bar:\n" + " baz: something\n" @@ -23,7 +21,7 @@ public void testBasicKeyValue() { } @Test - public void testNullKeyValue() { + void nullKeyValue() { String yaml = "foo:\n" + " ~: something\n"; @@ -33,7 +31,7 @@ public void testNullKeyValue() { } @Test - public void testListValue() { + void listValue() { String yaml = "foo:\n" + " bar:\n" + " ~:\n" @@ -47,22 +45,7 @@ public void testListValue() { } @Test - @Disabled - public void testListOfListValue() { - String yaml = "foo:\n" - + " bar:\n" - + " ~:\n" - + " - [cat, dog]\n" - + " - [mouse, rat]\n" - + " - [chicken, turkey]\n"; - - ConfigSource src = new YamlConfigSource("Yaml", yaml); - - assertEquals("cat\\,dog,mouse\\,rat,chicken\\,turkey", src.getValue("foo.bar")); - } - - @Test - public void testEmptyFile() { + void EmptyFile() { String yaml = ""; ConfigSource src = new YamlConfigSource("Yaml", yaml); assertNotNull(src, "Should create config source for empty file correctly"); diff --git a/sources/yaml/src/test/java/io/smallrye/config/source/yaml/YamlConfigSourceTest.java b/sources/yaml/src/test/java/io/smallrye/config/source/yaml/YamlConfigSourceTest.java index aef764194..5472b52df 100644 --- a/sources/yaml/src/test/java/io/smallrye/config/source/yaml/YamlConfigSourceTest.java +++ b/sources/yaml/src/test/java/io/smallrye/config/source/yaml/YamlConfigSourceTest.java @@ -2,14 +2,21 @@ import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.eclipse.microprofile.config.spi.Converter; import org.junit.jupiter.api.Test; import org.yaml.snakeyaml.Yaml; +import io.smallrye.config.SmallRyeConfig; +import io.smallrye.config.SmallRyeConfigBuilder; + public class YamlConfigSourceTest { @Test void flatten() throws Exception { @@ -34,6 +41,61 @@ void profiles() throws Exception { assertEquals("prod", yaml.getValue("%prod.foo.bar")); } + @Test + void list() { + String yaml = "quarkus:\n" + + " http:\n" + + " ssl:\n" + + " protocols:\n" + + " - TLSv1.2\n" + + " - TLSv1.3"; + + SmallRyeConfig config = new SmallRyeConfigBuilder().withSources(new YamlConfigSource("Yaml", yaml)).build(); + String[] values = config.getValue("quarkus.http.ssl.protocols", String[].class); + assertEquals(2, values.length); + assertEquals("TLSv1.2", values[0]); + assertEquals("TLSv1.3", values[1]); + + List list = config.getValues("quarkus.http.ssl.protocols", String.class, ArrayList::new); + assertEquals(2, list.size()); + assertEquals("TLSv1.2", list.get(0)); + assertEquals("TLSv1.3", list.get(1)); + } + + @Test + void config() throws Exception { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example-216.yml"))) + .withConverter(Users.class, 100, new UserConverter()) + .build(); + + final Users users = config.getValue("admin.users", Users.class); + assertEquals(2, users.getUsers().size()); + assertEquals(users.users.get(0).getEmail(), "joe@gmail.com"); + assertEquals(users.users.get(0).getRoles(), Stream.of("Moderator", "Admin").collect(toList())); + + assertEquals("joe@gmail.com", config.getRawValue("admin.users.[0].email")); + } + + @Test + void propertyNames() throws Exception { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example.yml"))) + .withConverter(Users.class, 100, new UserConverter()) + .build(); + + final List propertyNames = StreamSupport.stream(config.getPropertyNames().spliterator(), false) + .collect(toList()); + + assertTrue(propertyNames.contains("quarkus.http.port")); + assertTrue(propertyNames.contains("quarkus.http.ssl-port")); + assertTrue(propertyNames.contains("quarkus.http.ssl.protocols")); + assertFalse(propertyNames.contains("quarkus.http.ssl.protocols.[0]")); + assertEquals("TLSv1.2", config.getRawValue("quarkus.http.ssl.protocols.[0]")); + assertFalse(propertyNames.contains("quarkus.http.ssl.protocols.[1]")); + assertEquals("TLSv1.3", config.getRawValue("quarkus.http.ssl.protocols.[1]")); + } + public static class Users { List users; diff --git a/sources/yaml/src/test/resources/example.yml b/sources/yaml/src/test/resources/example.yml new file mode 100644 index 000000000..3bb3252f5 --- /dev/null +++ b/sources/yaml/src/test/resources/example.yml @@ -0,0 +1,22 @@ +quarkus: + http: + port: 8081 + ssl-port: 2443 + cors: + ~: true + access-control-max-age: 24H + exposed-headers: "SOME-HEADER" + methods: GET,PUT,POST,DELETE,OPTIONS + ssl: + protocols: + - TLSv1.2 + - TLSv1.3 + cipher-suites: + - TLS_AES_128_GCM_SHA256 + - TLS_AES_256_GCM_SHA384 + - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 + - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 + - TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 + - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 + swagger-ui: + always-include: true