Skip to content

Commit 518cab0

Browse files
Mathias Düsterhöftodrotbohm
authored andcommitted
DATAREST-885 - PatchOperation now evaluates SpEL expression.
Original pull request: spring-projects#223.
1 parent 3643e06 commit 518cab0

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Abstract base class representing and providing support methods for patch operations.
2727
*
2828
* @author Craig Walls
29+
* @author Mathias Düsterhöft
2930
*/
3031
public abstract class PatchOperation {
3132

@@ -164,7 +165,7 @@ protected Object getValueFromTarget(Object target) {
164165
* otherwise.
165166
*/
166167
protected <T> Object evaluateValueFromTarget(Object targetObject, Class<T> entityType) {
167-
return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(entityType) : value;
168+
return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(spelExpression.getValueType(targetObject)) : value;
168169
}
169170

170171
/**

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/ReplaceOperationTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*/
1616
package org.springframework.data.rest.webmvc.json.patch;
1717

18-
import static org.junit.Assert.*;
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertTrue;
1921

2022
import java.util.ArrayList;
2123
import java.util.List;
2224

2325
import org.junit.Test;
2426

27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
2529
public class ReplaceOperationTest {
2630

2731
@Test
@@ -38,6 +42,20 @@ public void replaceBooleanPropertyValue() throws Exception {
3842
assertTrue(todos.get(1).isComplete());
3943
}
4044

45+
@Test
46+
public void replaceObjectPropertyValue() throws Exception {
47+
// initial Todo list
48+
Todo todo = new Todo(1L, "A", false);
49+
50+
ReplaceOperation replace = new ReplaceOperation("/type", new JsonLateObjectEvaluator(new ObjectMapper().readTree("{\"value\":\"new\"}")));
51+
replace.perform(todo, Todo.class);
52+
53+
assertNotNull(todo.getType());
54+
assertNotNull(todo.getType().getValue());
55+
assertTrue(todo.getType().getValue().equals("new"));
56+
57+
}
58+
4159
@Test
4260
public void replaceTextPropertyValue() throws Exception {
4361
// initial Todo list

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/Todo.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@
1616

1717
package org.springframework.data.rest.webmvc.json.patch;
1818

19-
import lombok.AllArgsConstructor;
2019
import lombok.Data;
2120
import lombok.NoArgsConstructor;
2221

2322
/**
2423
* @author Roy Clarkson
2524
* @author Craig Walls
25+
* @author Mathias Düsterhöft
2626
*/
2727
@Data
2828
@NoArgsConstructor
29-
@AllArgsConstructor
3029
class Todo {
3130

3231
private Long id;
3332
private String description;
3433
private boolean complete;
34+
private TodoType type = new TodoType();
35+
36+
public Todo(Long id, String description, boolean complete) {
37+
this.id = id;
38+
this.description = description;
39+
this.complete = complete;
40+
}
3541
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.springframework.data.rest.webmvc.json.patch;
2+
3+
import javax.persistence.Embeddable;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
/**
10+
* @author Mathias Düsterhöft
11+
*/
12+
@Embeddable
13+
@Data
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class TodoType {
17+
private String value = "none";
18+
}

0 commit comments

Comments
 (0)