diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java index 59ccd15c4..fe2231d8d 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java @@ -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); } } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java index d520c269c..d9a66f157 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java @@ -140,7 +140,6 @@ public class JsonPatchPatchConverter implements PatchConverter { } 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()); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java index 15cebdb1c..c31911ca2 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java @@ -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()); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/Todo.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/Todo.java index a7c23d5d5..5046ea84c 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/Todo.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/Todo.java @@ -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 items = new ArrayList(); + private BigInteger amount; public Todo(Long id, String description, boolean complete) { diff --git a/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-biginteger.json b/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-biginteger.json new file mode 100644 index 000000000..9840124fc --- /dev/null +++ b/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-biginteger.json @@ -0,0 +1,3 @@ +[ + {"op":"replace", "path":"/amount", "value": 18446744073709551616 } +] \ No newline at end of file diff --git a/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-failing-with-invalid-content.json b/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-failing-with-invalid-content.json new file mode 100644 index 000000000..c74193262 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-failing-with-invalid-content.json @@ -0,0 +1,3 @@ +[ + {"op":"add", "path":"/description", "value": { "content" : "blabla"}} +] \ No newline at end of file