Browse Source

Fix bug 612 and add two testcases (#711)

* fix bug 612

* modify fix bug 612

* modify fix bug 612, add one more testcase

Co-authored-by: CindyChow123 <CindyChow123>
pull/732/head
Cincronic 4 years ago committed by GitHub
parent
commit
78a9420b42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java
  3. 37
      json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue612.java

28
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -239,11 +239,31 @@ public class JsonPath {
notNull(jsonObject, "json can not be null"); notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null"); notNull(configuration, "configuration can not be null");
notNull(mapFunction, "mapFunction can not be null"); notNull(mapFunction, "mapFunction can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); boolean optAsPathList = configuration.containsOption(AS_PATH_LIST);
for (PathRef updateOperation : evaluationContext.updateOperations()) { boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
updateOperation.convert(mapFunction, configuration); boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.convert(mapFunction, configuration);
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}catch (RuntimeException e) {
if (!optSuppressExceptions) {
throw e;
} else {
if (optAsPathList) {
return (T) configuration.jsonProvider().createArray();
} else {
if (optAlwaysReturnList) {
return (T) configuration.jsonProvider().createArray();
} else {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
}
}
} }
return resultByConfiguration(jsonObject, configuration, evaluationContext);
} }
/** /**

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

@ -146,8 +146,8 @@ public class JsonContext implements DocumentContext {
@Override @Override
public DocumentContext map(JsonPath path, MapFunction mapFunction) { public DocumentContext map(JsonPath path, MapFunction mapFunction) {
path.map(json, mapFunction, configuration); Object obj = path.map(json, mapFunction, configuration);
return this; return obj==null ? null:this;
} }
@Override @Override

37
json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue612.java

@ -0,0 +1,37 @@
package com.jayway.jsonpath.internal.function;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.jayway.jsonpath.*;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import org.junit.Assert;
import org.junit.Test;
import static com.jayway.jsonpath.JsonPath.using;
import static com.jayway.jsonpath.internal.path.PathCompiler.fail;
public class Issue612 {
@Test
public void test() {
Configuration config = Configuration.builder()
.options(Option.SUPPRESS_EXCEPTIONS)
.build();
String json = "{\"1\":{\"2\":null}}";
DocumentContext documentContext = JsonPath.using(config).parse(json);
JsonPath query = JsonPath.compile("$.1.2.a.b.c");
Assert.assertNull(documentContext.read(query));
Assert.assertNull(documentContext.map(query, (object, configuration) -> object));
}
@Test(expected = Exception.class)
public void test2() {
Configuration config = Configuration.builder()
.build();
String json = "{\"1\":{\"2\":null}}";
DocumentContext documentContext = JsonPath.using(config).parse(json);
JsonPath query = JsonPath.compile("$.1.2.a.b.c");
documentContext.map(query, (object, configuration) -> object);
}
}
Loading…
Cancel
Save