Browse Source

fixed issue 537 and added tests (#719)

pull/732/head
hezonghan 4 years ago committed by GitHub
parent
commit
727d9e05ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 29
      json-path/src/test/java/com/jayway/jsonpath/Issue_537.java

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

@ -323,7 +323,17 @@ public class JsonPath {
notNull(configuration, "configuration can not be null"); notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) { for (PathRef updateOperation : evaluationContext.updateOperations()) {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try {
updateOperation.renameKey(oldKeyName, newKeyName, configuration); updateOperation.renameKey(oldKeyName, newKeyName, configuration);
} catch (RuntimeException e) {
if(!optSuppressExceptions){
throw e;
}else{
// With option SUPPRESS_EXCEPTIONS,
// the PathNotFoundException should be ignored and the other updateOperation should be continued.
}
}
} }
return resultByConfiguration(jsonObject, configuration, evaluationContext); return resultByConfiguration(jsonObject, configuration, evaluationContext);
} }

29
json-path/src/test/java/com/jayway/jsonpath/Issue_537.java

@ -0,0 +1,29 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.List;
public class Issue_537 {
public static final Configuration jsonConf = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);
@Test
public void test_read(){ // originally passed
Object ans = JsonPath.using(jsonConf).parse("{}").read("missing");
assert(ans == null);
}
@Test
public void test_renameKey(){ // originally throws PathNotFoundException
List<Object> ans = JsonPath.using(jsonConf)
.parse("{\"list\":[" +
"{\"data\":{\"old\":1}}," +
"{\"data\":{}}," +
"{\"data\":{\"old\":2}}" +
"]}")
.renameKey("$..data", "old", "new")
.read("$.list");
assert(ans.toString().equals("[{\"data\":{\"new\":1}},{\"data\":{}},{\"data\":{\"new\":2}}]"));
}
}
Loading…
Cancel
Save