Skip to content

Commit c768bc1

Browse files
authored
Set result at the end of schema processing (#963)
1 parent 5069ba4 commit c768bc1

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
557557
if (results == null || results.isEmpty()) {
558558
// Do nothing if valid
559559
} else {
560-
executionContext.getResults().setResult(instanceLocation, v.getSchemaLocation(), v.getEvaluationPath(), false);
561560
if (errors == null) {
562561
errors = new LinkedHashSet<>();
563562
}
@@ -595,6 +594,11 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
595594
}
596595
}
597596

597+
if (errors != null && !errors.isEmpty()) {
598+
// Failed with assertion set result and drop all annotations from this schema
599+
// and all subschemas
600+
executionContext.getResults().setResult(instanceLocation, getSchemaLocation(), getEvaluationPath(), false);
601+
}
598602
return errors == null ? Collections.emptySet() : errors;
599603
}
600604

src/main/java/com/networknt/schema/result/JsonNodeResults.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public boolean isValid(JsonNodePath instanceLocation, JsonNodePath evaluationPat
4444
List<JsonNodeResult> instance = values.get(instanceLocation);
4545
if (instance != null) {
4646
for (JsonNodeResult result : instance) {
47-
if (evaluationPath.startsWith(result.getEvaluationPath().getParent())) {
47+
if (evaluationPath.startsWith(result.getEvaluationPath())) {
4848
if(!result.isValid()) {
4949
return false;
5050
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2024 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+
17+
package com.networknt.schema;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import java.util.List;
22+
import java.util.Set;
23+
import java.util.stream.Collectors;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
import com.networknt.schema.SpecVersion.VersionFlag;
28+
29+
/**
30+
* UnevaluatedPropertiesValidatorTest.
31+
*/
32+
public class UnevaluatedPropertiesValidatorTest {
33+
/**
34+
* Issue 962.
35+
*/
36+
@Test
37+
void annotationsOnlyDroppedAtTheEndOfSchemaProcessing() {
38+
String schemaData = "{\r\n"
39+
+ " \"type\": \"object\",\r\n"
40+
+ " \"required\": [\r\n"
41+
+ " \"key1\"\r\n"
42+
+ " ],\r\n"
43+
+ " \"properties\": {\r\n"
44+
+ " \"key1\": {\r\n"
45+
+ " \"type\": \"string\"\r\n"
46+
+ " },\r\n"
47+
+ " \"key2\": {\r\n"
48+
+ " \"type\": \"string\"\r\n"
49+
+ " },\r\n"
50+
+ " \"key3\": {\r\n"
51+
+ " \"type\": \"string\"\r\n"
52+
+ " }\r\n"
53+
+ " },\r\n"
54+
+ " \"unevaluatedProperties\": false\r\n"
55+
+ "}";
56+
String inputData = "{\r\n"
57+
+ " \"key2\": \"value2\",\r\n"
58+
+ " \"key3\": \"value3\",\r\n"
59+
+ " \"key4\": \"value4\"\r\n"
60+
+ "}";
61+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData);
62+
Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON);
63+
assertEquals(2, messages.size());
64+
List<ValidationMessage> assertions = messages.stream().collect(Collectors.toList());
65+
assertEquals("required", assertions.get(0).getType());
66+
assertEquals("key1", assertions.get(0).getProperty());
67+
assertEquals("unevaluatedProperties", assertions.get(1).getType());
68+
assertEquals("key4", assertions.get(1).getProperty());
69+
}
70+
}

src/test/resources/schema/unevaluatedTests/unevaluated-tests.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,9 @@
253253
},
254254
"valid": false,
255255
"validationMessages": [
256-
"$.vehicle: property 'pontoons' must not be unevaluated",
257-
"$.vehicle: property 'wings' must not be unevaluated"
256+
"$.vehicle: must be valid to one and only one schema, but 2 are valid",
257+
"$.vehicle: required property 'wheels' not found",
258+
"$.vehicle: required property 'headlights' not found"
258259
]
259260
},
260261
{
@@ -271,9 +272,10 @@
271272
},
272273
"valid": false,
273274
"validationMessages": [
275+
"$.vehicle: must be valid to one and only one schema, but 2 are valid",
274276
"$.vehicle: property 'invalid' must not be unevaluated",
275-
"$.vehicle: property 'pontoons' must not be unevaluated",
276-
"$.vehicle: property 'wings' must not be unevaluated"
277+
"$.vehicle: required property 'wheels' not found",
278+
"$.vehicle: required property 'headlights' not found"
277279
]
278280
},
279281
{
@@ -288,6 +290,7 @@
288290
},
289291
"valid": false,
290292
"validationMessages": [
293+
"$.vehicle: must be valid to one and only one schema, but 0 are valid",
291294
"$.vehicle: property 'invalid' must not be unevaluated"
292295
]
293296
}

0 commit comments

Comments
 (0)