Skip to content

Commit afbffe4

Browse files
committed
DATAREST-995 - Fixed detection of collection append operations for JSON 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.
1 parent db770fb commit afbffe4

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -189,7 +189,7 @@ private Integer targetListIndex(String path) {
189189
String[] pathNodes = path.split("\\/");
190190
String lastNode = pathNodes[pathNodes.length - 1];
191191

192-
if ("~".equals(lastNode)) {
192+
if (APPEND_CHARACTERS.contains(lastNode)) {
193193
return -1;
194194
}
195195

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.springframework.data.rest.webmvc.json.patch;
1717

1818
import java.util.Arrays;
19+
import java.util.List;
1920

2021
import org.springframework.expression.Expression;
2122
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -30,6 +31,7 @@
3031
public class PathToSpEL {
3132

3233
private static final SpelExpressionParser SPEL_EXPRESSION_PARSER = new SpelExpressionParser();
34+
static final List<String> APPEND_CHARACTERS = Arrays.asList("-", "~");
3335

3436
/**
3537
* Converts a patch path to an {@link Expression}.
@@ -55,7 +57,7 @@ public static Expression spelToExpression(String spel) {
5557
* Produces an expression targeting the parent of the object that the given path targets.
5658
*
5759
* @param path the path to find a parent expression for.
58-
* @return an {@link Expression} targeting the parent of the object specifed by path.
60+
* @return an {@link Expression} targeting the parent of the object specified by path.
5961
*/
6062
public static Expression pathToParentExpression(String path) {
6163
return spelToExpression(pathNodesToSpEL(copyOf(path.split("\\/"), path.split("\\/").length - 1)));
@@ -76,7 +78,7 @@ private static String pathNodesToSpEL(String[] pathNodes) {
7678
continue;
7779
}
7880

79-
if ("~".equals(pathNode)) {
81+
if (APPEND_CHARACTERS.contains(pathNode)) {
8082
spelBuilder.append("[size() - 1]");
8183
continue;
8284
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.rest.webmvc.json.patch;
1717

18+
import static org.hamcrest.CoreMatchers.*;
1819
import static org.junit.Assert.*;
1920

2021
import java.util.ArrayList;
@@ -73,4 +74,14 @@ public void addItemToList() throws Exception {
7374
assertEquals("C", todos.get(3).getDescription());
7475
assertFalse(todos.get(3).isComplete());
7576
}
77+
78+
@Test // DATAREST-995
79+
public void addsItemsToNestedList() {
80+
81+
Todo todo = new Todo(1L, "description", false);
82+
83+
new AddOperation("/items/-", "Some text.").perform(todo, Todo.class);
84+
85+
assertThat(todo.getItems().get(0), is("Some text."));
86+
}
7687
}

0 commit comments

Comments
 (0)