diff --git a/src/main/java/com/networknt/schema/TypeFactory.java b/src/main/java/com/networknt/schema/TypeFactory.java index b97d36a9..cb986053 100644 --- a/src/main/java/com/networknt/schema/TypeFactory.java +++ b/src/main/java/com/networknt/schema/TypeFactory.java @@ -75,6 +75,11 @@ public static JsonType getSchemaNodeType(JsonNode node) { * @return the json type */ public static JsonType getValueNodeType(JsonNode node, SchemaValidatorsConfig config) { + if (node == null) { + // This returns JsonType.UNKNOWN to be consistent with the behavior when + // JsonNodeType.MISSING + return JsonType.UNKNOWN; + } JsonNodeType type = node.getNodeType(); switch (type) { case OBJECT: diff --git a/src/test/java/com/networknt/schema/JsonWalkTest.java b/src/test/java/com/networknt/schema/JsonWalkTest.java index 99d73c6a..af1c2ac0 100644 --- a/src/test/java/com/networknt/schema/JsonWalkTest.java +++ b/src/test/java/com/networknt/schema/JsonWalkTest.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.MissingNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.networknt.schema.walk.JsonSchemaWalkListener; import com.networknt.schema.walk.WalkEvent; @@ -16,6 +17,7 @@ import java.util.Set; import java.util.TreeSet; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; class JsonWalkTest { @@ -110,6 +112,27 @@ void testWalkWithDifferentListeners() throws IOException { + "}"))); } + @Test + void testWalkMissingNodeWithPropertiesSchemaShouldNotThrow() { + String schemaContents = "{\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"field\": {\n" + + " \"anyOf\": [\n" + + " {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + " }"; + + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); + JsonSchema schema = factory.getSchema(schemaContents); + JsonNode missingNode = MissingNode.getInstance(); + assertDoesNotThrow(() -> schema.walk(missingNode, true)); + } + private InputStream getSchema() { return getClass().getClassLoader().getResourceAsStream("schema/walk-schema.json"); }