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 54631a6bf..afab4d0a0 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 @@ -47,9 +47,14 @@ public YamlConfigSource(String name, String str, int defaultOrdinal) { @SuppressWarnings("unchecked") private static Map streamToMap(InputStream inputStream) throws IOException { Assert.checkNotNullParam("inputStream", inputStream); - final Map yamlInput; + final Map yamlInput = new TreeMap<>(); try { - yamlInput = new Yaml().loadAs(inputStream, HashMap.class); + final Iterable objects = new Yaml().loadAll(inputStream); + for (Object object : objects) { + if (object instanceof Map) { + yamlInput.putAll(yamlInputToMap((Map) object)); + } + } inputStream.close(); } catch (Throwable t) { try { @@ -59,7 +64,7 @@ private static Map streamToMap(InputStream inputStream) throws I } throw t; } - return yamlInputToMap(yamlInput); + return yamlInput; } @SuppressWarnings("unchecked") 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 d08f38362..aef764194 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 @@ -24,6 +24,16 @@ void flatten() throws Exception { assertEquals("joe@gmail.com", yaml.getValue("admin.users.[0].email")); } + @Test + void profiles() throws Exception { + YamlConfigSource yaml = new YamlConfigSource("yaml", + YamlConfigSourceTest.class.getResourceAsStream("/example-profiles.yml")); + + assertEquals("default", yaml.getValue("foo.bar")); + assertEquals("dev", yaml.getValue("%dev.foo.bar")); + assertEquals("prod", yaml.getValue("%prod.foo.bar")); + } + public static class Users { List users; diff --git a/sources/yaml/src/test/resources/example-profiles.yml b/sources/yaml/src/test/resources/example-profiles.yml new file mode 100644 index 000000000..b4226a8c8 --- /dev/null +++ b/sources/yaml/src/test/resources/example-profiles.yml @@ -0,0 +1,14 @@ +--- +foo: + bar: + default +--- +"%dev": + foo: + bar: + dev +--- +"%prod": + foo: + bar: + prod