Skip to content

Commit

Permalink
Properly append all superfluous elements to an array during merge patch.
Browse files Browse the repository at this point in the history
Fixes GH-2357.
  • Loading branch information
odrotbohm committed Jan 19, 2024
1 parent 41de5ad commit 5c76446
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private boolean handleArrayNode(ArrayNode array, Collection<Object> collection,
? rawValues.apply(current)
: mapper.treeToValue(jsonNode, componentType.getType()));

break;
continue;
}

Object next = value.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,40 @@ void replacesNestedArrays() throws Exception {
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) {

Expand Down

0 comments on commit 5c76446

Please sign in to comment.