Skip to content

Commit ffe4ca0

Browse files
authored
Fix walk with validate when node is null (#1169)
1 parent 2cd2baa commit ffe4ca0

File tree

8 files changed

+133
-14
lines changed

8 files changed

+133
-14
lines changed

src/main/java/com/networknt/schema/AllOfValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
112112

113113
@Override
114114
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
115-
if (shouldValidateSchema) {
115+
if (shouldValidateSchema && node != null) {
116116
return validate(executionContext, node, rootNode, instanceLocation, true);
117117
}
118118
for (JsonSchema schema : this.schemas) {

src/main/java/com/networknt/schema/AnyOfValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ && canShortCircuit() && canShortCircuit(executionContext)) {
162162

163163
@Override
164164
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
165-
if (shouldValidateSchema) {
165+
if (shouldValidateSchema && node != null) {
166166
return validate(executionContext, node, rootNode, instanceLocation, true);
167167
}
168168
for (JsonSchema schema : this.schemas) {
169169
schema.walk(executionContext, node, rootNode, instanceLocation, false);
170170
}
171-
return new LinkedHashSet<>();
171+
return Collections.emptySet();
172172
}
173173

174174
/**

src/main/java/com/networknt/schema/NotValidator.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,11 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
6868

6969
@Override
7070
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
71-
if (shouldValidateSchema) {
71+
if (shouldValidateSchema && node != null) {
7272
return validate(executionContext, node, rootNode, instanceLocation, true);
7373
}
7474

75-
Set<ValidationMessage> errors = this.schema.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
76-
if (errors.isEmpty()) {
77-
return Collections.singleton(message().instanceNode(node).instanceLocation(instanceLocation)
78-
.locale(executionContext.getExecutionConfig().getLocale())
79-
.failFast(executionContext.isFailFast()).arguments(this.schema.toString())
80-
.build());
81-
}
82-
return Collections.emptySet();
75+
return this.schema.walk(executionContext, node, rootNode, instanceLocation, false);
8376
}
8477

8578
@Override

src/main/java/com/networknt/schema/OneOfValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ protected boolean canShortCircuit() {
230230
@Override
231231
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
232232
HashSet<ValidationMessage> validationMessages = new LinkedHashSet<>();
233-
if (shouldValidateSchema) {
233+
if (shouldValidateSchema && node != null) {
234234
validationMessages.addAll(validate(executionContext, node, rootNode, instanceLocation, true));
235235
} else {
236236
for (JsonSchema schema : this.schemas) {
237-
schema.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
237+
schema.walk(executionContext, node, rootNode, instanceLocation, false);
238238
}
239239
}
240240
return validationMessages;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,31 @@ void invalidTypeShouldThrowJsonSchemaException() {
3737
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
3838
assertEquals("type", ex.getValidationMessage().getMessageKey());
3939
}
40+
41+
@Test
42+
void walkValidationWithNullNodeShouldNotValidate() {
43+
String schemaContents = " {\r\n"
44+
+ " \"type\": \"object\",\r\n"
45+
+ " \"properties\": {\r\n"
46+
+ " \"prop1\": {\r\n"
47+
+ " \"allOf\": [\r\n"
48+
+ " {\r\n"
49+
+ " \"type\": \"string\"\r\n"
50+
+ " },\r\n"
51+
+ " {\r\n"
52+
+ " \"type\": \"integer\"\r\n"
53+
+ " }\r\n"
54+
+ " ]\r\n"
55+
+ " }\r\n"
56+
+ " },\r\n"
57+
+ " \"additionalProperties\": false\r\n"
58+
+ " }";
59+
60+
String jsonContents = "{}";
61+
62+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
63+
JsonSchema schema = factory.getSchema(schemaContents);
64+
ValidationResult result = schema.walk(jsonContents, InputFormat.JSON, true);
65+
assertEquals(true, result.getValidationMessages().isEmpty());
66+
}
4067
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,31 @@ void invalidTypeShouldThrowJsonSchemaException() {
3737
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
3838
assertEquals("type", ex.getValidationMessage().getMessageKey());
3939
}
40+
41+
@Test
42+
void walkValidationWithNullNodeShouldNotValidate() {
43+
String schemaContents = " {\r\n"
44+
+ " \"type\": \"object\",\r\n"
45+
+ " \"properties\": {\r\n"
46+
+ " \"prop1\": {\r\n"
47+
+ " \"anyOf\": [\r\n"
48+
+ " {\r\n"
49+
+ " \"type\": \"string\"\r\n"
50+
+ " },\r\n"
51+
+ " {\r\n"
52+
+ " \"type\": \"integer\"\r\n"
53+
+ " }\r\n"
54+
+ " ]\r\n"
55+
+ " }\r\n"
56+
+ " },\r\n"
57+
+ " \"additionalProperties\": false\r\n"
58+
+ " }";
59+
60+
String jsonContents = "{}";
61+
62+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
63+
JsonSchema schema = factory.getSchema(schemaContents);
64+
ValidationResult result = schema.walk(jsonContents, InputFormat.JSON, true);
65+
assertEquals(true, result.getValidationMessages().isEmpty());
66+
}
4067
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
class NotValidatorTest {
23+
@Test
24+
void walkValidationWithNullNodeShouldNotValidate() {
25+
String schemaContents = "{\r\n"
26+
+ " \"type\": \"object\",\r\n"
27+
+ " \"properties\": {\r\n"
28+
+ " \"prop1\": {\r\n"
29+
+ " \"not\": {\r\n"
30+
+ " \"type\": \"string\"\r\n"
31+
+ " }\r\n"
32+
+ " }\r\n"
33+
+ " },\r\n"
34+
+ " \"additionalProperties\": false\r\n"
35+
+ "}";
36+
37+
String jsonContents = "{}";
38+
39+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
40+
JsonSchema schema = factory.getSchema(schemaContents);
41+
ValidationResult result = schema.walk(jsonContents, InputFormat.JSON, true);
42+
assertEquals(true, result.getValidationMessages().isEmpty());
43+
}
44+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,32 @@ void oneOfDiscriminatorEnabledWithDiscriminatorInSubclass() {
469469
assertEquals(3, messages3.size());
470470
}
471471

472+
@Test
473+
void walkValidationWithNullNodeShouldNotValidate() {
474+
String schemaContents = " {\r\n"
475+
+ " \"type\": \"object\",\r\n"
476+
+ " \"properties\": {\r\n"
477+
+ " \"prop1\": {\r\n"
478+
+ " \"oneOf\": [\r\n"
479+
+ " {\r\n"
480+
+ " \"type\": \"string\"\r\n"
481+
+ " },\r\n"
482+
+ " {\r\n"
483+
+ " \"type\": \"integer\"\r\n"
484+
+ " }\r\n"
485+
+ " ]\r\n"
486+
+ " }\r\n"
487+
+ " },\r\n"
488+
+ " \"additionalProperties\": false\r\n"
489+
+ " }";
490+
491+
String jsonContents = "{}";
492+
493+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
494+
JsonSchema schema = factory.getSchema(schemaContents);
495+
ValidationResult result = schema.walk(jsonContents, InputFormat.JSON, true);
496+
result.getValidationMessages().forEach(m -> System.out.println(m));
497+
assertEquals(true, result.getValidationMessages().isEmpty());
498+
}
499+
472500
}

0 commit comments

Comments
 (0)