diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java index f28fb8191..f8b9ce3dc 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PatchOperation.java @@ -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; } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PathToSpEL.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PathToSpEL.java index abafd299b..2a231cda3 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PathToSpEL.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/PathToSpEL.java @@ -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 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; } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationTests.java index 95fcf981f..bd74f7e12 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/AddOperationTests.java @@ -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.")); + } }