From 365e4376c34a0f880ed4c5eceebf336f41e2f6b8 Mon Sep 17 00:00:00 2001 From: Oliver Trosien Date: Wed, 7 Sep 2016 12:48:24 +0200 Subject: [PATCH] DATAREST-885 - Support array values in JsonPatchPatchConverter. Original pull request: #226. --- .../webmvc/json/patch/JsonPatchPatchConverter.java | 5 ++--- .../data/rest/webmvc/json/patch/JsonPatchTests.java | 12 ++++++++++++ .../data/rest/webmvc/json/patch/Todo.java | 4 ++++ .../data/rest/webmvc/json/patch/patch-array.json | 3 +++ 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-array.json 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 27bff320f..d520c269c 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 @@ -34,6 +34,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; * @author Craig Walls * @author Oliver Gierke * @author Mathias Düsterhöft + * @author Oliver Trosien */ @RequiredArgsConstructor public class JsonPatchPatchConverter implements PatchConverter { @@ -136,10 +137,8 @@ public class JsonPatchPatchConverter implements PatchConverter { return valueNode.asInt(); } else if (valueNode.isLong()) { return valueNode.asLong(); - } else if (valueNode.isObject()) { + } else if (valueNode.isObject() || (valueNode.isArray())) { return new JsonLateObjectEvaluator(mapper, valueNode); - } else if (valueNode.isArray()) { - // TODO: Convert valueNode to array } return null; 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 4e8c62bbe..d2b063e7f 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 @@ -19,6 +19,7 @@ import static org.junit.Assert.*; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.junit.Test; @@ -114,6 +115,17 @@ public class JsonPatchTests { assertEquals("F", todos.get(5).getDescription()); } + @Test + public void patchArray() throws Exception { + Todo todo = new Todo(1L, "F", false); + + Patch patch = readJsonPatch("patch-array.json"); + assertEquals(1, patch.size()); + + Todo patchedTodo = patch.apply(todo, Todo.class); + assertEquals(Arrays.asList("one","two","three"), patchedTodo.getItems()); + } + 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 c3475c8e9..a7c23d5d5 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,9 @@ package org.springframework.data.rest.webmvc.json.patch; +import java.util.ArrayList; +import java.util.List; + import lombok.Data; import lombok.NoArgsConstructor; @@ -33,6 +36,7 @@ class Todo { private String description; private boolean complete; private TodoType type = new TodoType(); + private List items = new ArrayList(); 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-array.json b/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-array.json new file mode 100644 index 000000000..7611c0e80 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/resources/org/springframework/data/rest/webmvc/json/patch/patch-array.json @@ -0,0 +1,3 @@ +[ + {"op":"replace", "path":"/items", "value": ["one","two","three"]} +] \ No newline at end of file