Skip to content

Commit

Permalink
DATAREST-995 - Fixed detection of collection append operations for JS…
Browse files Browse the repository at this point in the history
…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
odrotbohm committed Feb 8, 2017
1 parent db770fb commit afbffe4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
@@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
@@ -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.
Expand All @@ -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;
Expand All @@ -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}.
Expand All @@ -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)));
Expand All @@ -76,7 +78,7 @@ private static String pathNodesToSpEL(String[] pathNodes) {
continue;
}

if ("~".equals(pathNode)) {
if (APPEND_CHARACTERS.contains(pathNode)) {
spelBuilder.append("[size() - 1]");
continue;
}
Expand Down
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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.