From 07039a283885ebc547e88c76cfd93065e27e8467 Mon Sep 17 00:00:00 2001 From: Thomas Mrozinski Date: Tue, 18 Feb 2020 18:48:36 -0500 Subject: [PATCH] DATAREST-1068 - Resize array during merge. When merging new data into a PersistentEntity, JsonNodes of array types were deserialized into immutable Lists, but the list is later mutated if the source and target arrays are of different sizes. This commit ensures that an array is deserialized into a mutable list. Original pull request: #371. --- .../rest/webmvc/json/DomainObjectReader.java | 3 ++- .../json/DomainObjectReaderUnitTests.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java index 5f8a6be3e..62c65efd8 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java @@ -62,6 +62,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; * @author Mark Paluch * @author Craig Andrews * @author Mathias Düsterhöft + * @author Thomas Mrozinski * @since 2.2 */ @RequiredArgsConstructor @@ -510,7 +511,7 @@ public class DomainObjectReader { } if (source.getClass().isArray()) { - return Arrays.asList((Object[]) source); + return new ArrayList<>(Arrays.asList((Object[]) source)); } return null; diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java index 434d67528..7d592a46e 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java @@ -73,6 +73,7 @@ import com.google.common.base.Charsets; * @author Craig Andrews * @author Mathias Düsterhöft * @author Ken Dombeck + * @author Thomas Mrozinski */ @RunWith(MockitoJUnitRunner.class) public class DomainObjectReaderUnitTests { @@ -102,6 +103,7 @@ public class DomainObjectReaderUnitTests { mappingContext.getPersistentEntity(SampleWithReference.class); mappingContext.getPersistentEntity(Note.class); mappingContext.getPersistentEntity(WithNullCollection.class); + mappingContext.getPersistentEntity(ArrayHolder.class); mappingContext.afterPropertiesSet(); this.entities = new PersistentEntities(Collections.singleton(mappingContext)); @@ -571,6 +573,16 @@ public class DomainObjectReaderUnitTests { assertThat(result.lastLogin).isNotNull(); assertThat(result.email).isEqualTo("foo@bar.com"); } + + @Test // DATAREST-1068 + public void arraysCanBeResizedDuringMerge() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + ArrayHolder target = new ArrayHolder(new String[] { }); + JsonNode node = mapper.readTree("{ \"array\" : [ \"new\" ] }"); + + ArrayHolder updated = reader.doMerge((ObjectNode) node, target, mapper); + assertThat(updated.array).containsExactly("new"); + } @SuppressWarnings("unchecked") private static T as(Object source, Class type) { @@ -795,4 +807,10 @@ public class DomainObjectReaderUnitTests { static class WithNullCollection { List strings; } + + // DATAREST-1068 + @Value + static class ArrayHolder { + String[] array; + } }