diff --git a/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParser.java b/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParser.java index 9dc379aa242..0baa1b3d64e 100644 --- a/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParser.java +++ b/config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigParser.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 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. @@ -149,7 +149,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(); 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 d386c9f35d3..7577f83c272 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, 2020 Oracle and/or its affiliates. + * 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. @@ -70,6 +70,7 @@ public HoconConfigParserBuilder resolveOptions(ConfigResolveOptions resolveOptio * * @return new instance of HOCON ConfigParser. */ + @Override public HoconConfigParser build() { return new HoconConfigParser(resolvingEnabled, resolveOptions); } 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 031a9938dfc..500a6bcb23e 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, 2020 Oracle and/or its affiliates. + * 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. @@ -207,7 +207,7 @@ public void testConfigKeyEscapedNameComplex() { Config config = Config .builder(ConfigSources.create(JSON, HoconConfigParser.MEDIA_TYPE_APPLICATION_JSON)) - .addParser(new HoconConfigParser()) + .addParser(HoconConfigParser.create()) .disableEnvironmentVariablesSource() .disableSystemPropertiesSource() .disableParserServices() @@ -252,7 +252,7 @@ public void testConfigKeyEscapedNameComplex() { @Test public void testGetSupportedMediaTypes() { - HoconConfigParser parser = new HoconConfigParser(); + HoconConfigParser parser = HoconConfigParser.create(); assertThat(parser.supportedMediaTypes(), is(not(empty()))); } @@ -261,7 +261,7 @@ public void testGetSupportedMediaTypes() { public void testCustomTypeMapping() { Config config = Config .builder(ConfigSources.create(AppType.DEF, HoconConfigParser.MEDIA_TYPE_APPLICATION_JSON)) - .addParser(new HoconConfigParser()) + .addParser(HoconConfigParser.create()) .addMapper(AppType.class, new AppTypeMapper()) .disableEnvironmentVariablesSource() .disableSystemPropertiesSource() @@ -277,6 +277,23 @@ public void testCustomTypeMapping() { } + @Test + void testParserFromJson() { + Config config = Config.builder() + .disableSystemPropertiesSource() + .disableEnvironmentVariablesSource() + .disableParserServices() + .addParser(HoconConfigParser.create()) + .addSource(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(""))); + } + // // helper // @@ -317,11 +334,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, @@ -365,15 +382,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..ed4f9ef6868 100644 --- a/config/hocon/src/test/resources/config.json +++ b/config/hocon/src/test/resources/config.json @@ -6,5 +6,8 @@ "oracle": { "com": "1", "cz": "2" + }, + "nulls": { + "null": null } }