Skip to content

NPE during JSON schema walk with validation #1151

@chrishodgins

Description

@chrishodgins

We ran into this while migrating to 1.5.5. A JSON schema walk returned an NPE rather than returning validation messages while validating a YAML document that did not have the corresponding properties. Below is the smallest recreate I've found that still fails with an NPE. I've replaced the YAML nodes with an instance of the MissingNode below to simulate the missing object properties.

import com.fasterxml.jackson.databind.node.MissingNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;

public class SchemaTest {

    public static void main(String[] args) {
        String schemaContents = """
            {
                "type": "object",
                "properties": {
                    "field": {
                    "anyOf": [
                        {
                        "type": "string"
                        }
                    ]
                    }
                }
            }
            """;

        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
        JsonSchema schema = factory.getSchema(schemaContents);
        schema.walk(MissingNode.getInstance(), true).getValidationMessages();
    }
}

Stack trace:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.getNodeType()" because "node" is null
        at com.networknt.schema.TypeFactory.getValueNodeType(TypeFactory.java:78)
        at com.networknt.schema.utils.JsonNodeUtil.equalsToSchemaType(JsonNodeUtil.java:70)
        at com.networknt.schema.TypeValidator.equalsToSchemaType(TypeValidator.java:50)
        at com.networknt.schema.AnyOfValidator.validate(AnyOfValidator.java:84)
        at com.networknt.schema.AnyOfValidator.walk(AnyOfValidator.java:166)
        at com.networknt.schema.JsonSchema.walk(JsonSchema.java:1278)
        at com.networknt.schema.PropertiesValidator.walkSchema(PropertiesValidator.java:197)
        at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:109)
        at com.networknt.schema.PropertiesValidator.walk(PropertiesValidator.java:133)
        at com.networknt.schema.JsonSchema.walk(JsonSchema.java:1278)
        at com.networknt.schema.JsonSchema.walkAtNodeInternal(JsonSchema.java:1247)
        at com.networknt.schema.JsonSchema.walk(JsonSchema.java:1082)
        at com.networknt.schema.JsonSchema.walk(JsonSchema.java:1162)
       ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions