Skip to content
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

Fix nullable issue #1134

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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: 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());
}
}