diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java index 2dce79d7..007f36bb 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java @@ -298,7 +298,9 @@ public abstract class PathRef implements Comparable { 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)); + } } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java b/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java index 0267590a..25c80960 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java @@ -3,9 +3,13 @@ package com.jayway.jsonpath; 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; @@ -55,7 +59,7 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest { context.put("$", "child", child1); ObjectNode node2 = context.read("$"); ObjectNode child2 = context.read("$.child"); - + assertThat(node1).isSameAs(node2); assertThat(child1).isSameAs(child2); } @@ -112,7 +116,32 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest { 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() { @@ -161,7 +190,7 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest { public static class Gen { public String eric; } - + public static final class Data { @JsonProperty("id") UUID id;