Skip to content

Commit

Permalink
Fix nullable issue (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay authored Nov 24, 2024
1 parent 45b4f09 commit a88b142
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/networknt/schema/utils/JsonNodeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ public static boolean equalsToSchemaType(JsonNode node, JsonType schemaType, Jso
}

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;
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/networknt/schema/TypeValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,36 @@ void walkNull() {
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());
}
}

0 comments on commit a88b142

Please sign in to comment.