Skip to content

Commit 4a056ad

Browse files
committed
Revert part of FasterXML#4008
1 parent ff29056 commit 4a056ad

File tree

2 files changed

+61
-70
lines changed

2 files changed

+61
-70
lines changed

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
}
Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,62 @@
11
package com.fasterxml.jackson.databind.node;
22

3-
3+
import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder;
4+
import static com.fasterxml.jackson.databind.BaseTest.a2q;
45
import com.fasterxml.jackson.core.JsonParser;
5-
import com.fasterxml.jackson.core.JsonProcessingException;
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import java.util.ArrayList;
99
import java.util.List;
10-
import java.util.stream.Collectors;
1110
import org.junit.jupiter.api.Assertions;
12-
import org.junit.jupiter.api.BeforeEach;
1311
import org.junit.jupiter.api.Test;
1412

15-
public class MissingValues4229Test {
16-
private static final String jsonString =
17-
"{\n"
18-
+ " \"target\": \"target1\"," // Found in <= 2.15.3 and 2.16.0\n
19-
+ " \"object1\": {\n"
20-
+ " \"target\": \"target2\"" // Found in <= 2.15.3, but not in 2.16.0\n
21-
+ " },\n"
22-
+ " \"object2\": {\n"
23-
+ " \"target\": {" // Found in <= 2.15.3, but not in 2.16.0
24-
+ " \"target\": \"ignoredAsParentIsTarget\""
25-
// Expect not to be found (as sub-tree search ends when parent is found)\n
26-
+ " }\n"
27-
+ " }\n"
28-
+ " }";
29-
30-
private JsonNode rootNode;
31-
32-
@BeforeEach
33-
public void init() throws JsonProcessingException {
34-
ObjectMapper objectMapper =
35-
new ObjectMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true);
36-
rootNode = objectMapper.readTree(jsonString);
37-
}
13+
// [databind#4229] JsonNode findValues and findParents missing expected values
14+
public class MissingValues4229Test
15+
{
16+
17+
private final String JSON = a2q("{"
18+
+ " 'target': 'target1'," // Found in <= 2.15.3 and 2.16.0
19+
+ " 'object1': {"
20+
+ " 'target': 'target2' " // Found in <= 2.15.3, but not in 2.16.0
21+
+ " },"
22+
+ " 'object2': {"
23+
+ " 'target': { " // Found in <= 2.15.3, but not in 2.16.0
24+
+ " 'target': 'ignoredAsParentIsTarget'" // Expect not to be found (as sub-tree search ends when parent is found)
25+
+ " }"
26+
+ " }"
27+
+ "}");
28+
29+
private final ObjectMapper objectMapper = jsonMapperBuilder()
30+
.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
31+
.build();
3832

3933
@Test
40-
public void testFindValues() {
41-
List<JsonNode> foundNodes = rootNode.findValues("target");
34+
public void testFindValues() throws Exception
35+
{
36+
JsonNode rootNode = objectMapper.readTree(JSON);
4237

43-
List<String> expectedNodePaths = new ArrayList<>();
44-
expectedNodePaths.add("/target");
45-
expectedNodePaths.add("/object1/target");
46-
expectedNodePaths.add("/object2/target");
38+
List<JsonNode> expectedNodes = new ArrayList<>();
39+
expectedNodes.add(rootNode.at("/target"));
40+
expectedNodes.add(rootNode.at("/object1/target"));
41+
expectedNodes.add(rootNode.at("/object2/target"));
4742

48-
List<JsonNode> expectedNodes = expectedNodePaths.stream().map(rootNode::at).collect(Collectors.toList());
43+
List<JsonNode> actualNodes = rootNode.findValues("target");
4944

50-
Assertions.assertEquals(expectedNodes, foundNodes);
45+
Assertions.assertEquals(expectedNodes, actualNodes);
5146
}
5247

5348
@Test
54-
public void testFindParents() {
55-
List<JsonNode> foundNodes = rootNode.findParents("target");
49+
public void testFindParents() throws Exception
50+
{
51+
JsonNode rootNode = objectMapper.readTree(JSON);
5652

5753
List<JsonNode> expectedNodes = new ArrayList<>();
5854
expectedNodes.add(rootNode.at(""));
5955
expectedNodes.add(rootNode.at("/object1"));
6056
expectedNodes.add(rootNode.at("/object2"));
6157

58+
List<JsonNode> foundNodes = rootNode.findParents("target");
59+
6260
Assertions.assertEquals(expectedNodes, foundNodes);
6361
}
6462
}

0 commit comments

Comments
 (0)