Skip to content

Commit da3865c

Browse files
authored
Fix NPE when walking a missing node that will have missing properties (#1152)
* Fix NPE when walking a missing node that will have missing properties * Change to make expectation clearer
1 parent 6c37935 commit da3865c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/main/java/com/networknt/schema/TypeFactory.java

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public static JsonType getSchemaNodeType(JsonNode node) {
7575
* @return the json type
7676
*/
7777
public static JsonType getValueNodeType(JsonNode node, SchemaValidatorsConfig config) {
78+
if (node == null) {
79+
// This returns JsonType.UNKNOWN to be consistent with the behavior when
80+
// JsonNodeType.MISSING
81+
return JsonType.UNKNOWN;
82+
}
7883
JsonNodeType type = node.getNodeType();
7984
switch (type) {
8085
case OBJECT:

src/test/java/com/networknt/schema/JsonWalkTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.MissingNode;
56
import com.fasterxml.jackson.databind.node.ObjectNode;
67
import com.networknt.schema.walk.JsonSchemaWalkListener;
78
import com.networknt.schema.walk.WalkEvent;
@@ -16,6 +17,7 @@
1617
import java.util.Set;
1718
import java.util.TreeSet;
1819

20+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1921
import static org.junit.jupiter.api.Assertions.assertEquals;
2022

2123
class JsonWalkTest {
@@ -110,6 +112,27 @@ void testWalkWithDifferentListeners() throws IOException {
110112
+ "}")));
111113
}
112114

115+
@Test
116+
void testWalkMissingNodeWithPropertiesSchemaShouldNotThrow() {
117+
String schemaContents = "{\n"
118+
+ " \"type\": \"object\",\n"
119+
+ " \"properties\": {\n"
120+
+ " \"field\": {\n"
121+
+ " \"anyOf\": [\n"
122+
+ " {\n"
123+
+ " \"type\": \"string\"\n"
124+
+ " }\n"
125+
+ " ]\n"
126+
+ " }\n"
127+
+ " }\n"
128+
+ " }";
129+
130+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
131+
JsonSchema schema = factory.getSchema(schemaContents);
132+
JsonNode missingNode = MissingNode.getInstance();
133+
assertDoesNotThrow(() -> schema.walk(missingNode, true));
134+
}
135+
113136
private InputStream getSchema() {
114137
return getClass().getClassLoader().getResourceAsStream("schema/walk-schema.json");
115138
}

0 commit comments

Comments
 (0)