Fix broken merge of nested arrays.

Fixes GH-2350.
This commit is contained in:
Oliver Drotbohm
2024-01-15 17:14:21 +01:00
parent ae851d878f
commit 6cec482bb4
2 changed files with 38 additions and 7 deletions

View File

@@ -444,15 +444,8 @@ public class DomainObjectReader {
TypeInformation<?> typeToMap = getTypeToMap(sourceValue, valueType);
if (value instanceof ObjectNode && sourceValue != null) {
doMerge((ObjectNode) value, sourceValue, mapper);
} else if (value instanceof ArrayNode && sourceValue != null) {
handleArray(value, sourceValue, mapper, getTypeToMap(sourceValue, typeToMap), null);
} else {
source.put(mappedKey, mapper.treeToValue(value, typeToMap.getType()));
}

View File

@@ -109,6 +109,7 @@ class DomainObjectReaderUnitTests {
mappingContext.getPersistentEntity(WithCustomMappedPrimitiveCollection.class);
mappingContext.getPersistentEntity(BugModel.class);
mappingContext.getPersistentEntity(ArrayListHolder.class);
mappingContext.getPersistentEntity(MapWrapper.class);
mappingContext.afterPropertiesSet();
this.entities = new PersistentEntities(Collections.singleton(mappingContext));
@@ -736,6 +737,39 @@ class DomainObjectReaderUnitTests {
assertThat(updated.array).containsExactly("ancient");
}
@Test // GH-2350
void replacesArrays() throws Exception {
ArrayHolder holder = new ArrayHolder(new String[] { "original" });
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree("{ \"array\" : [ \"first\", \"update\" ] }");
ArrayHolder result = reader.doMerge((ObjectNode) node, holder, mapper);
node = mapper.readTree("{ \"array\" : [ \"second\", \"update\" ] }");
result = reader.doMerge((ObjectNode) node, holder, mapper);
assertThat(result.getArray()).isEqualTo(new String[] { "second", "update" });
}
@Test // GH-2350
void replacesNestedArrays() throws Exception {
MapWrapper wrapper = new MapWrapper();
wrapper.map.put("array", new String[] { "original" });
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree("{ \"map\" : { \"array\" : [ \"first\", \"update\" ] } }");
MapWrapper result = reader.doMerge((ObjectNode) node, wrapper, mapper);
node = mapper.readTree("{ \"map\" : { \"array\" : [ \"second\", \"update\" ] } }");
result = reader.doMerge((ObjectNode) node, wrapper, mapper);
assertThat(result.map.get("array")).isEqualTo(new String[] { "second", "update" });
}
@SuppressWarnings("unchecked")
private static <T> T as(Object source, Class<T> type) {
@@ -1128,4 +1162,8 @@ class DomainObjectReaderUnitTests {
this.values = values;
}
}
static class MapWrapper {
public Map<String, Object> map = new HashMap<>();
}
}