DATAREST-537 - Fixed removing array elements in JSON Patch operations.
Before replacing the remove operation with a custom one that nulls out the object, we check whether the JSON Pointer points to an array element. If that's the case we skip the replacement and proceed.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2015 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.
|
||||
@@ -115,7 +115,8 @@ class JsonPatchHandler {
|
||||
|
||||
// Replace remove operation with replace operation and a value of null.
|
||||
JsonPointer path = (JsonPointer) ReflectionUtils.getField(PATH_FIELD, operation);
|
||||
patchedNode = new ReplaceOperation(path, NullNode.getInstance()).apply(patchedNode);
|
||||
patchedNode = isCollectionElementReference(path) ? operation.apply(patchedNode) : new ReplaceOperation(path,
|
||||
NullNode.getInstance()).apply(patchedNode);
|
||||
|
||||
} else {
|
||||
patchedNode = operation.apply(patchedNode);
|
||||
@@ -132,4 +133,28 @@ class JsonPatchHandler {
|
||||
<T> T applyPut(ObjectNode source, T existingObject) {
|
||||
return reader.readPut(source, existingObject, mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the trailing element of the given {@link JsonPointer} is a pointer into an array or collection.
|
||||
*
|
||||
* @param pointer must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private static boolean isCollectionElementReference(JsonPointer pointer) {
|
||||
|
||||
String[] segments = pointer.toString().split("/");
|
||||
|
||||
if (segments.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String trailing = segments[segments.length - 1];
|
||||
|
||||
try {
|
||||
Integer.parseInt(trailing);
|
||||
return true;
|
||||
} catch (NumberFormatException o_O) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2015 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,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.config;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.rest.webmvc.util.TestUtils.*;
|
||||
|
||||
@@ -96,4 +96,26 @@ public class JsonPatchHandlerUnitTests {
|
||||
assertThat(result.lastname, is(nullValue()));
|
||||
assertThat(result.address.zipCode, is("ZIP"));
|
||||
}
|
||||
|
||||
/**
|
||||
* DATAREST-537
|
||||
*/
|
||||
@Test
|
||||
public void removesArrayItemCorrectly() throws Exception {
|
||||
|
||||
User thomas = new User();
|
||||
thomas.firstname = "Thomas";
|
||||
|
||||
User christoph = new User();
|
||||
christoph.firstname = "Christoph";
|
||||
|
||||
this.user.colleagues = Arrays.asList(thomas, christoph);
|
||||
|
||||
String input = "[{ \"op\": \"remove\", \"path\": \"/colleagues/0\" }]";
|
||||
|
||||
handler.applyPatch(asStream(input), user);
|
||||
|
||||
assertThat(user.colleagues, hasSize(1));
|
||||
assertThat(user.colleagues.get(0).firstname, is(christoph.firstname));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user