From 399c846378a87647e88126f4ce2c0e4d80dd49ae Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 27 Apr 2020 23:45:58 +0200 Subject: [PATCH] DATAREST-1479 - Patching collections now also works for nested collections. We now properly use the PropertyPath's leaf property to check whether we need to deal with a collection in JSON Patch pointers. --- .../data/rest/webmvc/json/patch/SpelPath.java | 2 +- .../json/patch/AddOperationUnitTests.java | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/SpelPath.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/SpelPath.java index 2f0080b2d..964dabce6 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/SpelPath.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/SpelPath.java @@ -554,7 +554,7 @@ class SpelPath { return nested(segment); } - TypeInformation typeInformation = basePath.getTypeInformation(); + TypeInformation typeInformation = basePath.getLeafProperty().getTypeInformation(); if (typeInformation.isMap()) { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationUnitTests.java index d1837ddc8..b3e56f64c 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationUnitTests.java @@ -19,6 +19,10 @@ import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + import java.util.ArrayList; import java.util.List; @@ -137,4 +141,28 @@ public class AddOperationUnitTests { .withMessageContaining("2") // .withMessageContaining("1"); } + + @Test // DATAREST-1479 + public void manipulatesNestedCollectionProperly() { + + List todos = new ArrayList<>(); + todos.add(new Todo(1L, "A", false)); + todos.add(new Todo(2L, "B", false)); + + TodoList todoList = new TodoList(); + todoList.setTodos(todos); + TodoListWrapper outer = new TodoListWrapper(todoList); + + Todo newTodo = new Todo(3L, "C", false); + AddOperation.of("/todoList/todos/-", newTodo).perform(outer, TodoListWrapper.class); + + assertThat(outer.todoList.getTodos()).containsExactly(todos.get(0), todos.get(1), newTodo); + } + + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class TodoListWrapper { + public TodoList todoList; + } }