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.
This commit is contained in:
Thomas Mrozinski
2020-02-18 18:48:36 -05:00
committed by Oliver Drotbohm
parent 55a70093db
commit 07039a2838
2 changed files with 20 additions and 1 deletions

View File

@@ -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;

View File

@@ -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> T as(Object source, Class<T> type) {
@@ -795,4 +807,10 @@ public class DomainObjectReaderUnitTests {
static class WithNullCollection {
List<String> strings;
}
// DATAREST-1068
@Value
static class ArrayHolder {
String[] array;
}
}