diff --git a/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParserBuilder.java b/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParserBuilder.java index 1e8baea8f41..df7d05d617a 100644 --- a/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParserBuilder.java +++ b/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParserBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -92,6 +92,7 @@ public HoconConfigParserBuilder resolveOptions(ConfigResolveOptions resolveOptio * * @return new instance of HOCON ConfigParser. */ + @Override public ConfigParser build() { return new HoconConfigParser(resolvingEnabled, resolveOptions); } diff --git a/config/hocon/src/main/java/io/helidon/config/hocon/internal/HoconConfigParser.java b/config/hocon/src/main/java/io/helidon/config/hocon/internal/HoconConfigParser.java index 6d90ee8690b..ded6b89f92d 100644 --- a/config/hocon/src/main/java/io/helidon/config/hocon/internal/HoconConfigParser.java +++ b/config/hocon/src/main/java/io/helidon/config/hocon/internal/HoconConfigParser.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -131,7 +131,12 @@ private static ObjectNode fromConfig(ConfigObject config) { } else if (value instanceof ConfigObject) { builder.addObject(key, fromConfig((ConfigObject) value)); } else { - builder.addValue(key, value.unwrapped().toString()); + Object unwrapped = value.unwrapped(); + if (unwrapped == null) { + builder.addValue(key, ""); + } else { + builder.addValue(key, String.valueOf(unwrapped)); + } } }); return builder.build(); @@ -145,7 +150,12 @@ private static ListNode fromList(ConfigList list) { } else if (value instanceof ConfigObject) { builder.addObject(fromConfig((ConfigObject) value)); } else { - builder.addValue(value.unwrapped().toString()); + Object unwrapped = value.unwrapped(); + if (unwrapped == null) { + builder.addValue(""); + } else { + builder.addValue(String.valueOf(unwrapped)); + } } }); return builder.build(); 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 8d69948eaab..d6c4d48db7b 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2021 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. @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -47,6 +48,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -276,6 +278,26 @@ public void testCustomTypeMapping() { } + @Test + void testParserFromJson() { + Config config = Config.builder() + .disableSystemPropertiesSource() + .disableEnvironmentVariablesSource() + .disableParserServices() + .addParser(new HoconConfigParser()) + .sources(ConfigSources.classpath("config.json")) + .build(); + + Optional property = config.get("oracle.com").asString().asOptional(); + assertThat(property, is(Optional.of("1"))); + + property = config.get("nulls.null").asString().asOptional(); + assertThat(property, is(Optional.of(""))); + + List properties = config.get("nulls-array").asList(String.class).get(); + assertThat(properties, hasItems("test", "")); + } + // // helper // @@ -311,11 +333,11 @@ public static class AppType { + " storagePassphrase = \"${AES=thisIsEncriptedPassphrase}\"" + "}"; - private String greeting; - private String name; - private int pageSize; - private List basicRange; - private String storagePassphrase; + private final String greeting; + private final String name; + private final int pageSize; + private final List basicRange; + private final String storagePassphrase; public AppType( String name, @@ -359,15 +381,14 @@ private static class AppTypeMapper implements Function { @Override public AppType apply(Config config) throws ConfigMappingException, MissingValueException { - AppType app = new AppType( + + return new AppType( config.get("name").asString().get(), config.get("greeting").asString().get(), config.get("page-size").asInt().get(), config.get("basic-range").asList(Integer.class).get(), config.get("storagePassphrase").asString().get() ); - - return app; } } } diff --git a/config/hocon/src/test/resources/config.json b/config/hocon/src/test/resources/config.json index c21aa20fed7..9494f95de5d 100644 --- a/config/hocon/src/test/resources/config.json +++ b/config/hocon/src/test/resources/config.json @@ -6,5 +6,9 @@ "oracle": { "com": "1", "cz": "2" - } + }, + "nulls": { + "null": null + }, + "nulls-array": ["test", null] }