Skip to content

Fix NPE when walking a missing node that will have missing properties #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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