DATAREST-1039 - Fix appending a complex value appended to a collection via JSON Patch.

When a PATCH call using JSON Patch tried to append an item to an empty collection, it previously failed to look up the type to unmarshal the incoming payload to. We now inspect the declared types on the wrapped object's property to determine that type for append operations.

Also, an uninitialized collection is now initialized using Spring's CollectionFactory.
This commit is contained in:
Oliver Gierke
2017-05-12 14:32:48 +02:00
parent b0c19ce188
commit 9a9512767e
5 changed files with 80 additions and 2 deletions

View File

@@ -23,6 +23,9 @@ import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class AddOperationTests {
@Test
@@ -84,4 +87,28 @@ public class AddOperationTests {
assertThat(todo.getItems().get(0)).isEqualTo("Some text.");
}
@Test // DATAREST-1039
public void addsLazilyEvaluatedObjectToList() throws Exception {
Todo todo = new Todo(1L, "description", false);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree("\"Some text.\"");
JsonLateObjectEvaluator evaluator = new JsonLateObjectEvaluator(mapper, node);
new AddOperation("/items/-", evaluator).perform(todo, Todo.class);
assertThat(todo.getItems().get(0)).isEqualTo("Some text.");
}
@Test // DATAREST-1039
public void initializesNullCollectionsOnAppend() {
Todo todo = new Todo(1L, "description", false);
new AddOperation("/uninitialized/-", "Text").perform(todo, Todo.class);
assertThat(todo.getUninitialized()).containsExactly("Text");
}
}

View File

@@ -38,6 +38,7 @@ class Todo {
private boolean complete;
private TodoType type = new TodoType();
private List<String> items = new ArrayList<String>();
private List<String> uninitialized;
private BigInteger amount;
public Todo(Long id, String description, boolean complete) {