Skip to content

Commit a88b142

Browse files
authored
Fix nullable issue (#1134)
1 parent 45b4f09 commit a88b142

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/main/java/com/networknt/schema/utils/JsonNodeUtil.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ public static boolean equalsToSchemaType(JsonNode node, JsonType schemaType, Jso
8484
}
8585

8686
if (nodeType == JsonType.NULL) {
87-
if (parentSchema != null) {
87+
if (parentSchema != null && config.isNullableKeywordEnabled()) {
8888
JsonSchema grandParentSchema = parentSchema.getParentSchema();
89-
if (grandParentSchema != null
90-
&& JsonNodeUtil.isNodeNullable(grandParentSchema.getSchemaNode(), config)
89+
if (grandParentSchema != null && JsonNodeUtil.isNodeNullable(grandParentSchema.getSchemaNode())
9190
|| JsonNodeUtil.isNodeNullable(parentSchema.getSchemaNode())) {
9291
return true;
9392
}

src/test/java/com/networknt/schema/TypeValidatorTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,36 @@ void walkNull() {
149149
ValidationResult result = schema.walk(null, true);
150150
assertTrue(result.getValidationMessages().isEmpty());
151151
}
152+
153+
@Test
154+
void nullable() {
155+
String schemaData = "{\r\n"
156+
+ " \"$schema\":\"http://json-schema.org/draft-07/schema#\",\r\n"
157+
+ " \"type\":\"object\",\r\n"
158+
+ " \"properties\":{\r\n"
159+
+ " \"test\":{\r\n"
160+
+ " \"type\":\"object\",\r\n"
161+
+ " \"properties\":{\r\n"
162+
+ " \"nested\":{\r\n"
163+
+ " \"type\":\"string\",\r\n"
164+
+ " \"nullable\":true,\r\n"
165+
+ " \"format\":\"date\"\r\n"
166+
+ " }\r\n"
167+
+ " }\r\n"
168+
+ " }\r\n"
169+
+ " }\r\n"
170+
+ "}";
171+
String inputData = "{\r\n"
172+
+ " \"test\":{\r\n"
173+
+ " \"nested\":null\r\n"
174+
+ " }\r\n"
175+
+ "}";
176+
final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7);
177+
final JsonSchema validator = factory.getSchema(schemaData, SchemaValidatorsConfig.builder()
178+
.nullableKeywordEnabled(false)
179+
.build());
180+
181+
final Set<ValidationMessage> errors = validator.validate(inputData, InputFormat.JSON);
182+
assertEquals(1, errors.size());
183+
}
152184
}

0 commit comments

Comments
 (0)