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.
This commit is contained in:
Oliver Gierke
2017-02-08 17:48:34 +01:00
parent e94ba4a1fa
commit f4b8ee3be2
3 changed files with 19 additions and 6 deletions

View File

@@ -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 @@ public abstract class PatchOperation {
String[] pathNodes = path.split("\\/");
String lastNode = pathNodes[pathNodes.length - 1];
if ("~".equals(lastNode)) {
if (APPEND_CHARACTERS.contains(lastNode)) {
return -1;
}

View File

@@ -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 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
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 class PathToSpEL {
* 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 @@ public class PathToSpEL {
continue;
}
if ("~".equals(pathNode)) {
if (APPEND_CHARACTERS.contains(pathNode)) {
spelBuilder.append("[size() - 1]");
continue;
}

View File

@@ -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 class AddOperationTests {
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."));
}
}