Skip to content

Commit

Permalink
Fix #3122
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 17, 2021
1 parent 1869295 commit 21b69df
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,7 @@ Asaf Romano (asaf-romano@github)
David Hoffman (dhofftgt@github)
* Contributed #3082: Dont track unknown props in buffer if `ignoreAllUnknown` is true
(2.13.0)

Eric Sirianni (sirianni@github)
* Reported #3122: Deep merge for `JsonNode` using `ObjectReader.readTree()`
(2.13.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Project: jackson-databind
#3099: Optimize "AnnotatedConstructor.call()" case by passing explicit null
#3101: Add AnnotationIntrospector.XmlExtensions interface for decoupling javax dependencies
#3117: Use more limiting default visibility settings for JDK types (java.*, javax.*)
#3122: Deep merge for `JsonNode` using `ObjectReader.readTree()`
(reported by Eric S)
- Fix to avoid problem with `BigDecimalNode`, scale of `Integer.MIN_VALUE` (see
[dataformats-binary#264] for details)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/ObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,12 @@ protected final JsonNode _bindAndCloseAsTree(JsonParser p0) throws IOException {

protected final JsonNode _bindAsTree(JsonParser p) throws IOException
{
// 16-Apr-2021, tatu: Should usually NOT be called this way but
// as per [databind#3122] should still work
if (_valueToUpdate != null) {
return (JsonNode) _bind(p, _valueToUpdate);
}

// Need to inline `_initForReading()` due to tree reading handling end-of-input specially
_config.initialize(p);
if (_schema != null) {
Expand Down Expand Up @@ -2098,6 +2104,13 @@ protected final JsonNode _bindAsTree(JsonParser p) throws IOException
*/
protected final JsonNode _bindAsTreeOrNull(JsonParser p) throws IOException
{
// 16-Apr-2021, tatu: Should usually NOT be called this way but
// as per [databind#3122] should still work
if (_valueToUpdate != null) {
return (JsonNode) _bind(p, _valueToUpdate);
}

// Need to inline `_initForReading()` (as per above)
_config.initialize(p);
if (_schema != null) {
p.setSchema(_schema);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fasterxml.jackson.databind.deser.merge;

import com.fasterxml.jackson.annotation.JsonMerge;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -162,4 +162,38 @@ public void testUpdateArrayWithString() throws Exception

assertEquals(a2q("{'test':'n/a'}"), result.toString());
}

// [databind#3122]: "readTree()" fails where "readValue()" doesn't:
public void testObjectDeepMerge3122() throws Exception
{
final String jsonToMerge = a2q("{'root':{'b':'bbb','foo':'goodbye'}}");

JsonNode node1 = MAPPER.readTree(a2q("{'root':{'a':'aaa','foo':'hello'}}"));
assertEquals(2, node1.path("root").size());

JsonNode node2 = MAPPER.readerForUpdating(node1)
.readValue(jsonToMerge);
assertSame(node1, node2);
assertEquals(3, node1.path("root").size());

node1 = MAPPER.readTree(a2q("{'root':{'a':'aaa','foo':'hello'}}"));
JsonNode node3 = MAPPER.readerForUpdating(node1)
.readTree(jsonToMerge);
assertSame(node1, node3);
assertEquals(3, node1.path("root").size());

node1 = MAPPER.readTree(a2q("{'root':{'a':'aaa','foo':'hello'}}"));
JsonNode node4 = MAPPER.readerForUpdating(node1)
.readTree(utf8Bytes(jsonToMerge));
assertSame(node1, node4);
assertEquals(3, node1.path("root").size());

// And finally variant passing `JsonParser`
try (JsonParser p = MAPPER.createParser(jsonToMerge)) {
JsonNode node5 = MAPPER.readerForUpdating(node1)
.readTree(p);
assertSame(node1, node5);
assertEquals(3, node1.path("root").size());
}
}
}

0 comments on commit 21b69df

Please sign in to comment.