Skip to content

Commit

Permalink
Fix broken merge of nested arrays.
Browse files Browse the repository at this point in the history
Fixes GH-2350.
  • Loading branch information
odrotbohm committed Jan 15, 2024
1 parent ae851d8 commit 6cec482
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,8 @@ private void doMergeNestedMap(Map<Object, Object> source, ObjectNode node, Objec
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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void setUp() {
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));
Expand Down Expand Up @@ -736,6 +737,39 @@ void arraysCanRemoveElementsDuringMerge() throws Exception {
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) {

Expand Down Expand Up @@ -1128,4 +1162,8 @@ public void setValues(Collection<String> values) {
this.values = values;
}
}

static class MapWrapper {
public Map<String, Object> map = new HashMap<>();
}
}

0 comments on commit 6cec482

Please sign in to comment.