jackson-coreutils icon indicating copy to clipboard operation
jackson-coreutils copied to clipboard

NullPointerException in NodeType.getNodeType when validating an empty string against a schema

Open jeffngo opened this issue 7 years ago • 1 comments

In jackson-core-2.9.3, NodeType.getNodeType fails the Preconditions check with a NullPointerException if the JsonToken is not in the TOKEN_MAP:

    /**
     * Given a {@link JsonNode} as an argument, return its type. The argument
     * MUST NOT BE NULL, and MUST NOT be a {@link MissingNode}
     *
     * @param node the node to determine the type of
     * @return the type for this node
     */
    public static NodeType getNodeType(final JsonNode node)
    {
        final JsonToken token = node.asToken();
        final NodeType ret = TOKEN_MAP.get(token);

        Preconditions.checkNotNull(ret, "unhandled token type " + token);

        return ret;
    }

Here is a simple unit test I wrote to test the different versions of jackson-core.

private static final String STATUS_OUTPUT_SCHEMA =
      "{ \"enum\": [\"" + STATUS_ACTIVE_EXIT + "\", \"" + STATUS_INACTIVE_EXIT + "\"] }";

@Test
  public void testEmptyStringAgainstSchema() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode schema = mapper.readTree(STATUS_OUTPUT_SCHEMA);
    JsonSchemaUtils.isValid(mapper.readTree(""), schema);
  }

In version 2.6.4 of jackson-core, this would fail with a JsonMappingException. However, in version 2.9.3 of jackson-core, this will trigger a NPE. Here is the stacktrace:

com.delphix.appliance.node.logging.exception.ConstructingFatalThrowableException: java.lang.NullPointerException: unhandled token type NOT_AVAILABLE
	at com.delphix.appliance.server.util.ExceptionInstrumentationUtil.handleFatalThrowable(ExceptionInstrumentationUtil.java:193)
	at com.delphix.appliance.server.util.ExceptionInstrumentationUtil.access$000(ExceptionInstrumentationUtil.java:32)
	at com.delphix.appliance.server.util.ExceptionInstrumentationUtil$2.sample(ExceptionInstrumentationUtil.java:163)
	at com.delphix.appliance.server.util.ExceptionInstrumentationUtil$2.sample(ExceptionInstrumentationUtil.java:159)
	at com.google.monitoring.runtime.instrumentation.ConstructorInstrumenter.invokeSamplers(ConstructorInstrumenter.java:207)
	at java.lang.RuntimeException.<init>(RuntimeException.java:63)
	at java.lang.NullPointerException.<init>(NullPointerException.java:70)
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
	at com.github.fge.jackson.NodeType.getNodeType(NodeType.java:144)
	at com.github.fge.jsonschema.processors.data.SchemaContext.<init>(SchemaContext.java:49)
	at com.github.fge.jsonschema.processors.validation.InstanceValidator.process(InstanceValidator.java:104)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:56)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:34)
	at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79)
	at com.github.fge.jsonschema.main.JsonSchema.doValidate(JsonSchema.java:76)
	at com.github.fge.jsonschema.main.JsonSchema.validInstance(JsonSchema.java:182)

jeffngo avatar Jan 20 '18 19:01 jeffngo

The problem does not exists in version 2.9.2 of jackson-core. The problem appears in version 2.9.3.

jlolling avatar Oct 29 '18 22:10 jlolling