Permalink
Browse files

DATAREST-995 - Fixed detection of collection append operations for JS…

…ON Patch requests.

We previously erroneously looked for a ~ in a JSON Pointer to indicate collection append in e.g. an add operation. We now also support the actually correct - keeping the original behavior for now to not break clients that currently make use of it.
  • Loading branch information...
1 parent db770fb commit afbffe462e8144cb3e9d23931cfb13b5e352c1a3 @olivergierke olivergierke committed Feb 8, 2017
@@ -1,5 +1,5 @@
/*
- * Copyright 2014-2016 the original author or authors.
+ * Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -189,7 +189,7 @@ private Integer targetListIndex(String path) {
String[] pathNodes = path.split("\\/");
String lastNode = pathNodes[pathNodes.length - 1];
- if ("~".equals(lastNode)) {
+ if (APPEND_CHARACTERS.contains(lastNode)) {
return -1;
}
@@ -1,5 +1,5 @@
/*
- * Copyright 2014-2016 the original author or authors.
+ * Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.data.rest.webmvc.json.patch;
import java.util.Arrays;
+import java.util.List;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -30,6 +31,7 @@
public class PathToSpEL {
private static final SpelExpressionParser SPEL_EXPRESSION_PARSER = new SpelExpressionParser();
+ static final List<String> APPEND_CHARACTERS = Arrays.asList("-", "~");
/**
* Converts a patch path to an {@link Expression}.
@@ -55,7 +57,7 @@ public static Expression spelToExpression(String spel) {
* Produces an expression targeting the parent of the object that the given path targets.
*
* @param path the path to find a parent expression for.
- * @return an {@link Expression} targeting the parent of the object specifed by path.
+ * @return an {@link Expression} targeting the parent of the object specified by path.
*/
public static Expression pathToParentExpression(String path) {
return spelToExpression(pathNodesToSpEL(copyOf(path.split("\\/"), path.split("\\/").length - 1)));
@@ -76,7 +78,7 @@ private static String pathNodesToSpEL(String[] pathNodes) {
continue;
}
- if ("~".equals(pathNode)) {
+ if (APPEND_CHARACTERS.contains(pathNode)) {
spelBuilder.append("[size() - 1]");
continue;
}
@@ -1,5 +1,5 @@
/*
- * Copyright 2014-2016 the original author or authors.
+ * Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package org.springframework.data.rest.webmvc.json.patch;
+import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.ArrayList;
@@ -73,4 +74,14 @@ public void addItemToList() throws Exception {
assertEquals("C", todos.get(3).getDescription());
assertFalse(todos.get(3).isComplete());
}
+
+ @Test // DATAREST-995
+ public void addsItemsToNestedList() {
+
+ Todo todo = new Todo(1L, "description", false);
+
+ new AddOperation("/items/-", "Some text.").perform(todo, Todo.class);
+
+ assertThat(todo.getItems().get(0), is("Some text."));
+ }
}

0 comments on commit afbffe4

Please sign in to comment.