Skip to content

Commit 509940f

Browse files
committed
Fix NPE when walking a missing node that will have missing properties
1 parent 6c37935 commit 509940f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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;
@@ -110,6 +111,26 @@ void testWalkWithDifferentListeners() throws IOException {
110111
+ "}")));
111112
}
112113

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

0 commit comments

Comments
 (0)