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 1 commit
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
21 changes: 21 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 Down Expand Up @@ -110,6 +111,26 @@ void testWalkWithDifferentListeners() throws IOException {
+ "}")));
}

@Test
void testWalkMissingNodeWithPropertiesSchema() {
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);
schema.walk(MissingNode.getInstance(), true).getValidationMessages();
}

private InputStream getSchema() {
return getClass().getClassLoader().getResourceAsStream("schema/walk-schema.json");
}
Expand Down