Skip to content

Commit e90e6b8

Browse files
committed
Merge branch '2.16' into 2.17
2 parents f039d16 + a0538bb commit e90e6b8

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Project: jackson-databind
2222
#4216: Primitive array deserializer cannot being captured by `DeserializerModifier`
2323
(reported by @SakuraKoi)
2424
(fix contributed by Joo-Hyuk K)
25+
#4229 JsonNode findValues and findParents missing expected values in 2.16.0
26+
(reported by @gcookemoto)
27+
(fix contributed by Joo-Hyuk K)
2528

2629
2.16.0 (15-Nov-2023)
2730

src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -382,37 +382,32 @@ public JsonNode findValue(String propertyName)
382382
@Override
383383
public List<JsonNode> findValues(String propertyName, List<JsonNode> foundSoFar)
384384
{
385-
JsonNode jsonNode = _children.get(propertyName);
386-
if (jsonNode != null) {
387-
if (foundSoFar == null) {
388-
foundSoFar = new ArrayList<>();
385+
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
386+
if (propertyName.equals(entry.getKey())) {
387+
if (foundSoFar == null) {
388+
foundSoFar = new ArrayList<JsonNode>();
389+
}
390+
foundSoFar.add(entry.getValue());
391+
} else { // only add children if parent not added
392+
foundSoFar = entry.getValue().findValues(propertyName, foundSoFar);
389393
}
390-
foundSoFar.add(jsonNode);
391-
return foundSoFar;
392-
}
393-
394-
// only add children if parent not added
395-
for (JsonNode child : _children.values()) {
396-
foundSoFar = child.findValues(propertyName, foundSoFar);
397394
}
398395
return foundSoFar;
399396
}
400397

401398
@Override
402399
public List<String> findValuesAsText(String propertyName, List<String> foundSoFar)
403400
{
404-
JsonNode jsonNode = _children.get(propertyName);
405-
if (jsonNode != null) {
406-
if (foundSoFar == null) {
407-
foundSoFar = new ArrayList<>();
401+
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
402+
if (propertyName.equals(entry.getKey())) {
403+
if (foundSoFar == null) {
404+
foundSoFar = new ArrayList<String>();
405+
}
406+
foundSoFar.add(entry.getValue().asText());
407+
} else { // only add children if parent not added
408+
foundSoFar = entry.getValue().findValuesAsText(propertyName,
409+
foundSoFar);
408410
}
409-
foundSoFar.add(jsonNode.asText());
410-
return foundSoFar;
411-
}
412-
413-
// only add children if parent not added
414-
for (JsonNode child : _children.values()) {
415-
foundSoFar = child.findValuesAsText(propertyName, foundSoFar);
416411
}
417412
return foundSoFar;
418413
}
@@ -436,18 +431,16 @@ public ObjectNode findParent(String propertyName)
436431
@Override
437432
public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar)
438433
{
439-
JsonNode jsonNode = _children.get(propertyName);
440-
if (jsonNode != null) {
441-
if (foundSoFar == null) {
442-
foundSoFar = new ArrayList<>();
434+
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
435+
if (propertyName.equals(entry.getKey())) {
436+
if (foundSoFar == null) {
437+
foundSoFar = new ArrayList<JsonNode>();
438+
}
439+
foundSoFar.add(this);
440+
} else { // only add children if parent not added
441+
foundSoFar = entry.getValue()
442+
.findParents(propertyName, foundSoFar);
443443
}
444-
foundSoFar.add(this);
445-
return foundSoFar;
446-
}
447-
448-
// only add children if parent not added
449-
for (JsonNode child : _children.values()) {
450-
foundSoFar = child.findParents(propertyName, foundSoFar);
451444
}
452445
return foundSoFar;
453446
}

src/test/java/com/fasterxml/jackson/databind/node/TestFindMethods.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,32 @@
44

55
import com.fasterxml.jackson.databind.BaseMapTest;
66
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
78

89
public class TestFindMethods
910
extends BaseMapTest
1011
{
12+
private final String JSON_SAMPLE = "{ \"a\" : { \"value\" : 3 },"
13+
+"\"array\" : [ { \"b\" : 3 }, {\"value\" : 42}, { \"other\" : true } ]"
14+
+"}";
15+
16+
private final String JSON_4229 = a2q("{"
17+
+ " 'target': 'target1'," // Found in <= 2.15.3 and 2.16.0
18+
+ " 'object1': {"
19+
+ " 'target': 'target2' " // Found in <= 2.15.3, but not in 2.16.0
20+
+ " },"
21+
+ " 'object2': {"
22+
+ " 'target': { " // Found in <= 2.15.3, but not in 2.16.0
23+
+ " 'target': 'ignoredAsParentIsTarget'" // Expect not to be found (as sub-tree search ends when parent is found)
24+
+ " }"
25+
+ " }"
26+
+ "}");
27+
28+
private final ObjectMapper MAPPER = newJsonMapper();
29+
1130
public void testNonMatching() throws Exception
1231
{
13-
JsonNode root = _buildTree();
32+
JsonNode root = MAPPER.readTree(JSON_SAMPLE);
1433

1534
assertNull(root.findValue("boogaboo"));
1635
assertNull(root.findParent("boogaboo"));
@@ -24,7 +43,7 @@ public void testNonMatching() throws Exception
2443

2544
public void testMatchingSingle() throws Exception
2645
{
27-
JsonNode root = _buildTree();
46+
JsonNode root = MAPPER.readTree(JSON_SAMPLE);
2847

2948
JsonNode node = root.findValue("b");
3049
assertNotNull(node);
@@ -38,7 +57,7 @@ public void testMatchingSingle() throws Exception
3857

3958
public void testMatchingMultiple() throws Exception
4059
{
41-
JsonNode root = _buildTree();
60+
JsonNode root = MAPPER.readTree(JSON_SAMPLE);
4261

4362
List<JsonNode> nodes = root.findValues("value");
4463
assertEquals(2, nodes.size());
@@ -61,11 +80,25 @@ public void testMatchingMultiple() throws Exception
6180
assertEquals("42", values.get(1));
6281
}
6382

64-
private JsonNode _buildTree() throws Exception
83+
// [databind#4229]: regression in 2.16.0
84+
public void testFindValues4229() throws Exception
6585
{
66-
final String SAMPLE = "{ \"a\" : { \"value\" : 3 },"
67-
+"\"array\" : [ { \"b\" : 3 }, {\"value\" : 42}, { \"other\" : true } ]"
68-
+"}";
69-
return objectMapper().readTree(SAMPLE);
86+
JsonNode rootNode = MAPPER.readTree(JSON_4229);
87+
assertEquals(Arrays.asList(
88+
rootNode.at("/target"),
89+
rootNode.at("/object1/target"),
90+
rootNode.at("/object2/target")),
91+
rootNode.findValues("target"));
92+
}
93+
94+
// [databind#4229]: regression in 2.16.0
95+
public void testFindParents4229() throws Exception
96+
{
97+
JsonNode rootNode = MAPPER.readTree(JSON_4229);
98+
assertEquals(Arrays.asList(
99+
rootNode,
100+
rootNode.at("/object1"),
101+
rootNode.at("/object2")),
102+
rootNode.findParents("target"));
70103
}
71104
}

0 commit comments

Comments
 (0)