Browse Source

Using the more approtiate PathNotFoundException if old key in rename map was not found.

pull/81/head
Tamas Adam 9 years ago
parent
commit
3cc47259fa
  1. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  2. 2
      json-path/src/test/java/com/jayway/jsonpath/WriteTest.java

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

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

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

@ -246,7 +246,7 @@ public class WriteTest extends BaseTest {
parse(JSON_DOCUMENT).renameKey("$.store.book[*]['author', 'category']", "old-key", "new-key");
}
@Test(expected = InvalidPathException.class)
@Test(expected = PathNotFoundException.class)
public void non_existent_key_rename_not_allowed(){
Object o = parse(JSON_DOCUMENT).renameKey("$", "fake", "new-fake").json();
}

Loading…
Cancel
Save