Skip to content

Commit

Permalink
Fix issue with null value in JSON. (#2728)
Browse files Browse the repository at this point in the history
* Fix issue with null value in JSON.

Resolves #2720

* Fix copyright errors

* fix compiler errors

* fix null value in arrays
  • Loading branch information
romain-grecourt authored Feb 4, 2021
1 parent 7d5e915 commit f5a734a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -92,6 +92,7 @@ public HoconConfigParserBuilder resolveOptions(ConfigResolveOptions resolveOptio
*
* @return new instance of HOCON ConfigParser.
*/
@Override
public ConfigParser 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, 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.
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<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("")));

List<String> properties = config.get("nulls-array").asList(String.class).get();
assertThat(properties, hasItems("test", ""));
}

//
// helper
//
Expand Down Expand Up @@ -311,11 +333,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 @@ -359,15 +381,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;
}
}
}
6 changes: 5 additions & 1 deletion config/hocon/src/test/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"oracle": {
"com": "1",
"cz": "2"
}
},
"nulls": {
"null": null
},
"nulls-array": ["test", null]
}

0 comments on commit f5a734a

Please sign in to comment.