Browse Source

Merge pull request #478 from BenFradet/issue/438

Do not map non-existing values, fixes #438
pull/517/head
kallestenflo 6 years ago committed by GitHub
parent
commit
e230406154
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  2. 29
      json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

4
json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java

@ -298,7 +298,9 @@ public abstract class PathRef implements Comparable<PathRef> {
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));
}
}
}

29
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;
@ -113,6 +117,31 @@ 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() {

Loading…
Cancel
Save