Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue #537 (renameKey) and added tests #719

Merged
merged 1 commit into from Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Expand Up @@ -308,7 +308,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}}]"));
}
}