Browse Source

Fix for rename on non-existing old key - an InvalidPathException is thrown.

pull/81/head
Tamas Adam 10 years ago
parent
commit
dbff1d8b48
  1. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  2. 5
      json-path/src/test/java/com/jayway/jsonpath/WriteTest.java

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

@ -2,6 +2,7 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidModificationException; import com.jayway.jsonpath.InvalidModificationException;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProvider;
@ -53,6 +54,9 @@ public abstract class PathRef implements Comparable<PathRef> {
protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){ protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){
if(configuration.jsonProvider().isMap(targetMap)){ if(configuration.jsonProvider().isMap(targetMap)){
if(configuration.jsonProvider().getMapValue(targetMap, oldKeyName) == JsonProvider.UNDEFINED){
throw new InvalidPathException("Key "+oldKeyName+" not found in map!");
}
configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName)); configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName));
configuration.jsonProvider().removeProperty(targetMap, oldKeyName); configuration.jsonProvider().removeProperty(targetMap, oldKeyName);
} else { } else {

5
json-path/src/test/java/com/jayway/jsonpath/WriteTest.java

@ -245,4 +245,9 @@ public class WriteTest extends BaseTest {
public void multiple_properties_cannot_be_renamed(){ public void multiple_properties_cannot_be_renamed(){
parse(JSON_DOCUMENT).renameKey("$.store.book[*]['author', 'category']", "old-key", "new-key"); parse(JSON_DOCUMENT).renameKey("$.store.book[*]['author', 'category']", "old-key", "new-key");
} }
@Test(expected = InvalidPathException.class)
public void non_existent_key_rename_not_allowed(){
Object o = parse(JSON_DOCUMENT).renameKey("$", "fake", "new-fake").json();
}
} }
Loading…
Cancel
Save