Properly append all superfluous elements to an array during merge patch.

Fixes GH-2357.
This commit is contained in:
Oliver Drotbohm
2024-01-19 18:24:20 +01:00
parent d293891190
commit 9ab41e7d01
2 changed files with 35 additions and 1 deletions

View File

@@ -390,7 +390,7 @@ public class DomainObjectReader {
? rawValues.apply(current)
: mapper.treeToValue(jsonNode, componentType.getType()));
break;
continue;
}
Object next = value.next();

View File

@@ -776,6 +776,40 @@ class DomainObjectReaderUnitTests {
assertThat(result.map.get("array")).isEqualTo(new String[] { "second", "update" });
}
@Test // GH-2357
void addsElementToPreviouslyEmptyCollectionForPatch() throws Exception {
Child child = new Child();
child.items = new ArrayList<>();
JsonNode node = new ObjectMapper()
.readTree("{ \"items\" : [ { \"some\" : \"value\" }, { \"some\" : \"otherValue\" } ] }");
Child result = reader.doMerge((ObjectNode) node, child, new ObjectMapper());
assertThat(result.items).hasSize(2);
assertThat(result.items.get(0).some).isEqualTo("value");
assertThat(result.items.get(1).some).isEqualTo("otherValue");
}
@Test // GH-2357
void augmentsCollectionForPatch() throws Exception {
Child child = new Child();
child.items = new ArrayList<>(Arrays.asList(new Item("old")));
JsonNode node = new ObjectMapper()
.readTree(
"{ \"items\" : [ { \"some\" : \"value\" }, { \"some\" : \"otherValue\" }, { \"some\" : \"yetAnotherValue\" } ] }");
Child result = reader.doMerge((ObjectNode) node, child, new ObjectMapper());
assertThat(result.items).hasSize(3);
assertThat(result.items.get(0).some).isEqualTo("value");
assertThat(result.items.get(1).some).isEqualTo("otherValue");
assertThat(result.items.get(2).some).isEqualTo("yetAnotherValue");
}
@SuppressWarnings("unchecked")
private static <T> T as(Object source, Class<T> type) {