DATAREST-889 - JsonPatchPatchConverter explicitly rejects unknown paths and values.

JsonPatchPatchConverter now explicitly rejects problematic JSON Patch payloads (e.g. incorrect paths, incorrect values) throwing a PatchException.

Original pull request: #227.
This commit is contained in:
Oliver Trosien
2016-09-07 12:48:24 +02:00
committed by Oliver Gierke
parent 1513482b9c
commit 48125faea3
6 changed files with 41 additions and 3 deletions

View File

@@ -42,7 +42,7 @@ class JsonLateObjectEvaluator implements LateObjectEvaluator {
try {
return mapper.readValue(valueNode.traverse(), type);
} catch (Exception e) {
return null;
throw new PatchException("JSON deserialization exception", e);
}
}
}

View File

@@ -140,7 +140,6 @@ public class JsonPatchPatchConverter implements PatchConverter<JsonNode> {
} else if (valueNode.isObject() || (valueNode.isArray())) {
return new JsonLateObjectEvaluator(mapper, valueNode);
}
return null;
throw new PatchException("Unrecognized valueNode type at path: " + path + ". valueNode: " + valueNode.toString());
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.rest.webmvc.json.patch;
import static org.junit.Assert.*;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -131,6 +132,36 @@ public class JsonPatchTests {
assertEquals(Arrays.asList("one", "two", "three"), patchedTodo.getItems());
}
@Test
public void patchUnknownType() throws Exception {
Todo todo = new Todo();
todo.setAmount(BigInteger.ONE);
try {
Patch patch = readJsonPatch("patch-biginteger.json");
assertEquals(1, patch.size());
assertEquals(new BigInteger("18446744073709551616"), patch.getOperations().get(0).value);
} catch (PatchException e) {
assertEquals("Unrecognized valueNode type at path: /amount. valueNode: 18446744073709551616", e.getMessage());
}
}
@Test
public void failureWithInvalidPatchContent() throws Exception {
Todo todo = new Todo();
todo.setDescription("Description");
Patch patch = readJsonPatch("patch-failing-with-invalid-content.json");
assertEquals(1, patch.size());
try {
Todo patchedTodo = patch.apply(todo, Todo.class);
assertEquals("Description", patchedTodo.getDescription());
} catch (PatchException e) {
assertEquals("JSON deserialization exception", e.getMessage());
}
}
private Patch readJsonPatch(String jsonPatchFile) throws IOException, JsonParseException, JsonMappingException {
ClassPathResource resource = new ClassPathResource(jsonPatchFile, getClass());

View File

@@ -16,6 +16,7 @@
package org.springframework.data.rest.webmvc.json.patch;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
@@ -37,6 +38,7 @@ class Todo {
private boolean complete;
private TodoType type = new TodoType();
private List<String> items = new ArrayList<String>();
private BigInteger amount;
public Todo(Long id, String description, boolean complete) {

View File

@@ -0,0 +1,3 @@
[
{"op":"replace", "path":"/amount", "value": 18446744073709551616 }
]

View File

@@ -0,0 +1,3 @@
[
{"op":"add", "path":"/description", "value": { "content" : "blabla"}}
]