DATAREST-885 - PatchOperation now evaluates SpEL expression.

Original pull request: #223.
This commit is contained in:
Mathias Düsterhöft
2016-09-06 15:24:12 +02:00
committed by Oliver Gierke
parent c2f88f26dd
commit 8de60dccda
4 changed files with 47 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.expression.ExpressionException;
* Abstract base class representing and providing support methods for patch operations. * Abstract base class representing and providing support methods for patch operations.
* *
* @author Craig Walls * @author Craig Walls
* @author Mathias Düsterhöft
*/ */
public abstract class PatchOperation { public abstract class PatchOperation {
@@ -164,7 +165,7 @@ public abstract class PatchOperation {
* otherwise. * otherwise.
*/ */
protected <T> Object evaluateValueFromTarget(Object targetObject, Class<T> entityType) { protected <T> Object evaluateValueFromTarget(Object targetObject, Class<T> entityType) {
return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(entityType) : value; return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(spelExpression.getValueType(targetObject)) : value;
} }
/** /**

View File

@@ -15,13 +15,17 @@
*/ */
package org.springframework.data.rest.webmvc.json.patch; 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.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ReplaceOperationTest { public class ReplaceOperationTest {
@Test @Test
@@ -38,6 +42,20 @@ public class ReplaceOperationTest {
assertTrue(todos.get(1).isComplete()); 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 @Test
public void replaceTextPropertyValue() throws Exception { public void replaceTextPropertyValue() throws Exception {
// initial Todo list // initial Todo list

View File

@@ -16,20 +16,26 @@
package org.springframework.data.rest.webmvc.json.patch; package org.springframework.data.rest.webmvc.json.patch;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/** /**
* @author Roy Clarkson * @author Roy Clarkson
* @author Craig Walls * @author Craig Walls
* @author Mathias Düsterhöft
*/ */
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor
class Todo { class Todo {
private Long id; private Long id;
private String description; private String description;
private boolean complete; 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;
}
} }

View File

@@ -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";
}