Skip to content

Commit

Permalink
Fix NPE when walking a missing node that will have missing properties (
Browse files Browse the repository at this point in the history
…#1152)

* Fix NPE when walking a missing node that will have missing properties

* Change to make expectation clearer
  • Loading branch information
justin-tay authored Feb 7, 2025
1 parent 6c37935 commit da3865c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/networknt/schema/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/networknt/schema/JsonWalkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit da3865c

Please sign in to comment.