DATAREST-1273 - Properly support adding to collections via indexes in JsonPatch expressions.
We now support adding a to a collection via JsonPatch expressions by using the collection's size as target index. Contrary to paths pointing to existing collection elements we cannot determine the type of the object to be created. Thus, we fall back to the common element type of all existing collection elements. We also reject indexes pointing to elements with an index greater than the current collection's size.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.json.patch;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -111,4 +112,29 @@ public class AddOperationUnitTests {
|
||||
|
||||
assertThat(todo.getUninitialized()).containsExactly("Text");
|
||||
}
|
||||
|
||||
@Test // DATAREST-1273
|
||||
public void addsItemToTheEndOfACollectionViaIndex() {
|
||||
|
||||
List<Todo> todos = new ArrayList<Todo>();
|
||||
todos.add(new Todo(1L, "A", false));
|
||||
|
||||
Todo todo = new Todo(2L, "B", true);
|
||||
AddOperation.of("/1", todo).perform(todos, Todo.class);
|
||||
|
||||
assertThat(todos).element(1).isEqualTo(todo);
|
||||
}
|
||||
|
||||
@Test // DATAREST-1273
|
||||
public void rejectsAdditionBeyondEndOfList() {
|
||||
|
||||
List<Todo> todos = new ArrayList<Todo>();
|
||||
todos.add(new Todo(1L, "A", false));
|
||||
|
||||
assertThatExceptionOfType(PatchException.class) //
|
||||
.isThrownBy(() -> AddOperation.of("/2", new Todo(2L, "B", true)).perform(todos, Todo.class)) //
|
||||
.withMessageContaining("index") //
|
||||
.withMessageContaining("2") //
|
||||
.withMessageContaining("1");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user