From 727d9e05ba9538b83e8dbee1bf16c8665766a0b5 Mon Sep 17 00:00:00 2001 From: hezonghan <46273682+hezonghan@users.noreply.github.com> Date: Wed, 2 Jun 2021 04:37:25 -0700 Subject: [PATCH] fixed issue 537 and added tests (#719) --- .../java/com/jayway/jsonpath/JsonPath.java | 12 +++++++- .../java/com/jayway/jsonpath/Issue_537.java | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/Issue_537.java diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 3e280673..9fb78b1f 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -323,7 +323,17 @@ public class JsonPath { 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); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/Issue_537.java b/json-path/src/test/java/com/jayway/jsonpath/Issue_537.java new file mode 100644 index 00000000..481e51e6 --- /dev/null +++ b/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 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}}]")); + } +}