From 518cab040fd3319d5e2bcae613c082bd76f816da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Du=CC=88sterho=CC=88ft?= Date: Tue, 6 Sep 2016 15:24:12 +0200 Subject: [PATCH] DATAREST-885 - PatchOperation now evaluates SpEL expression. Original pull request: #223. --- .../webmvc/json/patch/PatchOperation.java | 3 ++- .../json/patch/ReplaceOperationTest.java | 20 ++++++++++++++++++- .../data/rest/webmvc/json/patch/Todo.java | 10 ++++++++-- .../data/rest/webmvc/json/patch/TodoType.java | 18 +++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/TodoType.java diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java index 774d1e769..8005e5a1a 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java @@ -26,6 +26,7 @@ import org.springframework.expression.ExpressionException; * Abstract base class representing and providing support methods for patch operations. * * @author Craig Walls + * @author Mathias Düsterhöft */ public abstract class PatchOperation { @@ -164,7 +165,7 @@ public abstract class PatchOperation { * otherwise. */ protected Object evaluateValueFromTarget(Object targetObject, Class entityType) { - return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(entityType) : value; + return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(spelExpression.getValueType(targetObject)) : value; } /** diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/ReplaceOperationTest.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/ReplaceOperationTest.java index 1a6ad281f..5d33d8d58 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/ReplaceOperationTest.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/ReplaceOperationTest.java @@ -15,13 +15,17 @@ */ package org.springframework.data.rest.webmvc.json.patch; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import org.junit.Test; +import com.fasterxml.jackson.databind.ObjectMapper; + public class ReplaceOperationTest { @Test @@ -38,6 +42,20 @@ public class ReplaceOperationTest { assertTrue(todos.get(1).isComplete()); } + @Test + public void replaceObjectPropertyValue() throws Exception { + // initial Todo list + Todo todo = new Todo(1L, "A", false); + + ReplaceOperation replace = new ReplaceOperation("/type", new JsonLateObjectEvaluator(new ObjectMapper().readTree("{\"value\":\"new\"}"))); + replace.perform(todo, Todo.class); + + assertNotNull(todo.getType()); + assertNotNull(todo.getType().getValue()); + assertTrue(todo.getType().getValue().equals("new")); + + } + @Test public void replaceTextPropertyValue() throws Exception { // initial Todo list 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 01fee5d11..edf221e1a 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,20 +16,26 @@ package org.springframework.data.rest.webmvc.json.patch; -import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @author Roy Clarkson * @author Craig Walls + * @author Mathias Düsterhöft */ @Data @NoArgsConstructor -@AllArgsConstructor class Todo { private Long id; private String description; private boolean complete; + private TodoType type = new TodoType(); + + public Todo(Long id, String description, boolean complete) { + this.id = id; + this.description = description; + this.complete = complete; + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/TodoType.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/TodoType.java new file mode 100644 index 000000000..c773cafc9 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/TodoType.java @@ -0,0 +1,18 @@ +package org.springframework.data.rest.webmvc.json.patch; + +import javax.persistence.Embeddable; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author Mathias Düsterhöft + */ +@Embeddable +@Data +@AllArgsConstructor +@NoArgsConstructor +public class TodoType { + private String value = "none"; +}