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; + } }