Skip to content

Commit

Permalink
Fix issue with null value in JSON. (#2723)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Langer <[email protected]>
  • Loading branch information
tomas-langer authored Feb 3, 2021
1 parent b4c1161 commit b90650a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -70,6 +70,7 @@ public HoconConfigParserBuilder resolveOptions(ConfigResolveOptions resolveOptio
*
* @return new instance of HOCON ConfigParser.
*/
@Override
public HoconConfigParser build() {
return new HoconConfigParser(resolvingEnabled, resolveOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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())));
}
Expand All @@ -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()
Expand All @@ -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<String> 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
//
Expand Down Expand Up @@ -317,11 +334,11 @@ public static class AppType {
+ " storagePassphrase = \"${AES=thisIsEncriptedPassphrase}\""
+ "}";

private String greeting;
private String name;
private int pageSize;
private List<Integer> basicRange;
private String storagePassphrase;
private final String greeting;
private final String name;
private final int pageSize;
private final List<Integer> basicRange;
private final String storagePassphrase;

public AppType(
String name,
Expand Down Expand Up @@ -365,15 +382,14 @@ private static class AppTypeMapper implements Function<Config, AppType> {

@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;
}
}
}
3 changes: 3 additions & 0 deletions config/hocon/src/test/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"oracle": {
"com": "1",
"cz": "2"
},
"nulls": {
"null": null
}
}

0 comments on commit b90650a

Please sign in to comment.