Skip to content

Commit

Permalink
fixed issue 537 and added tests (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
hezonghan committed Jun 2, 2021
1 parent eed1cb2 commit 727d9e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Expand Up @@ -323,7 +323,17 @@ public <T> T renameKey(Object jsonObject, String oldKeyName, String newKeyName,
notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.renameKey(oldKeyName, newKeyName, configuration);
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try {
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);
}
Expand Down
29 changes: 29 additions & 0 deletions 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}}]"));
}
}

0 comments on commit 727d9e0

Please sign in to comment.