Repository: networknt/json-schema-validator (java)

Issue:
Validator not respecting json schema validation Specification standard 
Given this schema:
```
{
   "$schema":"http://json-schema.org/draft-07/schema#",
   "type":"object",
   "properties":{
      "test":{
         "type":"object",
         "properties":{
            "nested":{
               "type":"string",
               "nullable":true,
               "format":"date"
            }
         }
      }
   }
}
```

This input:
```
{
  "test":{
      "nested":null
  }
}
```

And this java snippet

```
final JsonNode jsonNodeSchema = ObjectMapperUtil.readTree(SCHEMA);
final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNodeSchema));
final JsonSchema validator = factory.getSchema(jsonNodeSchema, SchemaValidatorsConfig.builder()
    .nullableKeywordEnabled(false)
    .build());

final JsonNode jsonNodeFromString = ObjectMapperUtil.readTree(DATA);
final Set<ValidationMessage> errors = validator.validate(jsonNodeFromString);
if (!errors.isEmpty()) {
    throw new RuntimeException(errors.toString());
}
```

Expectation:
An error message to always be shown, irrespective of 'nullable': "/test/nested: null found, string expected"
This is because in the json schema specification the 'nullable' keyword does not exist and setting it to either true/false should have no effect on the validation.

Actual result: 
setting the 'nullable' flag in the schema to true: no error message
setting the 'nullable' flag in the schema to false: error message shown "/test/nested: null found, string expected"

Using the same json schema and input on the following site would yield the correct error:
https://www.jsonschemavalidator.net/

Pull request:
Fix nullable issue
Closes #1133

Patch:
diff --git a/src/main/java/com/networknt/schema/utils/JsonNodeUtil.java b/src/main/java/com/networknt/schema/utils/JsonNodeUtil.java
index 083efc0..df2b91b 100644
--- a/src/main/java/com/networknt/schema/utils/JsonNodeUtil.java
+++ b/src/main/java/com/networknt/schema/utils/JsonNodeUtil.java
@@ -84,10 +84,9 @@ public class JsonNodeUtil {
             }
 
             if (nodeType == JsonType.NULL) {
-                if (parentSchema != null) {
+                if (parentSchema != null && config.isNullableKeywordEnabled()) {
                     JsonSchema grandParentSchema = parentSchema.getParentSchema();
-                    if (grandParentSchema != null
-                            && JsonNodeUtil.isNodeNullable(grandParentSchema.getSchemaNode(), config)
+                    if (grandParentSchema != null && JsonNodeUtil.isNodeNullable(grandParentSchema.getSchemaNode())
                             || JsonNodeUtil.isNodeNullable(parentSchema.getSchemaNode())) {
                         return true;
                     }


Test patch:
diff --git a/src/test/java/com/networknt/schema/TypeValidatorTest.java b/src/test/java/com/networknt/schema/TypeValidatorTest.java
index 5924290..3687d3e 100644
--- a/src/test/java/com/networknt/schema/TypeValidatorTest.java
+++ b/src/test/java/com/networknt/schema/TypeValidatorTest.java
@@ -149,4 +149,36 @@ class TypeValidatorTest {
         ValidationResult result = schema.walk(null, true);
         assertTrue(result.getValidationMessages().isEmpty());
     }
+
+    @Test
+    void nullable() {
+        String schemaData = "{\r\n"
+                + "   \"$schema\":\"http://json-schema.org/draft-07/schema#\",\r\n"
+                + "   \"type\":\"object\",\r\n"
+                + "   \"properties\":{\r\n"
+                + "      \"test\":{\r\n"
+                + "         \"type\":\"object\",\r\n"
+                + "         \"properties\":{\r\n"
+                + "            \"nested\":{\r\n"
+                + "               \"type\":\"string\",\r\n"
+                + "               \"nullable\":true,\r\n"
+                + "               \"format\":\"date\"\r\n"
+                + "            }\r\n"
+                + "         }\r\n"
+                + "      }\r\n"
+                + "   }\r\n"
+                + "}";
+        String inputData = "{\r\n"
+                + "  \"test\":{\r\n"
+                + "      \"nested\":null\r\n"
+                + "  }\r\n"
+                + "}";
+        final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7);
+        final JsonSchema validator = factory.getSchema(schemaData, SchemaValidatorsConfig.builder()
+            .nullableKeywordEnabled(false)
+            .build());
+
+        final Set<ValidationMessage> errors = validator.validate(inputData, InputFormat.JSON);
+        assertEquals(1, errors.size());
+    }
 }