Skip to content

Commit

Permalink
Merge pull request #478 from BenFradet/issue/438
Browse files Browse the repository at this point in the history
Do not map non-existing values, fixes #438
  • Loading branch information
kallestenflo committed Dec 13, 2018
2 parents f2dc68e + 0f6eaa7 commit e230406
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ public void set(Object newVal, Configuration configuration){
public void convert(MapFunction mapFunction, Configuration configuration) {
for (String property : properties) {
Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration));
if (currentValue != JsonProvider.UNDEFINED) {
configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingException;
import org.junit.Test;

Expand Down Expand Up @@ -55,7 +59,7 @@ public void always_return_same_object() { // Test because of Bug #211
context.put("$", "child", child1);
ObjectNode node2 = context.read("$");
ObjectNode child2 = context.read("$.child");

assertThat(node1).isSameAs(node2);
assertThat(child1).isSameAs(child2);
}
Expand Down Expand Up @@ -112,7 +116,32 @@ public void test_type_ref_fail() throws IOException {

using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef);
}


@Test
public void mapPropertyWithPOJO() {
String someJson = "" +
"{\n" +
" \"a\": \"a\",\n" +
" \"b\": \"b\"\n" +
"}";
ObjectMapper om = new ObjectMapper();
om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Configuration c = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonNodeJsonProvider(om))
.build();
DocumentContext context = JsonPath.using(c).parse(someJson);
String someJsonStr = context.jsonString();
DocumentContext altered = context.map("$['a', 'b', 'c']", new MapFunction() {
@Override
public Object map(Object currentValue, Configuration configuration) {
return currentValue;
}
});
assertThat(altered.jsonString()).isEqualTo(someJsonStr);
}

@Test
// https://github.com/json-path/JsonPath/issues/364
public void setPropertyWithPOJO() {
Expand Down Expand Up @@ -161,7 +190,7 @@ public static class FooBarBaz<T> {
public static class Gen {
public String eric;
}

public static final class Data {
@JsonProperty("id")
UUID id;
Expand Down

0 comments on commit e230406

Please sign in to comment.