Skip to content

Commit

Permalink
Set result at the end of schema processing (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay authored Feb 8, 2024
1 parent 5069ba4 commit c768bc1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
if (results == null || results.isEmpty()) {
// Do nothing if valid
} else {
executionContext.getResults().setResult(instanceLocation, v.getSchemaLocation(), v.getEvaluationPath(), false);
if (errors == null) {
errors = new LinkedHashSet<>();
}
Expand Down Expand Up @@ -595,6 +594,11 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public boolean isValid(JsonNodePath instanceLocation, JsonNodePath evaluationPat
List<JsonNodeResult> instance = values.get(instanceLocation);
if (instance != null) {
for (JsonNodeResult result : instance) {
if (evaluationPath.startsWith(result.getEvaluationPath().getParent())) {
if (evaluationPath.startsWith(result.getEvaluationPath())) {
if(!result.isValid()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.networknt.schema;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

import com.networknt.schema.SpecVersion.VersionFlag;

/**
* UnevaluatedPropertiesValidatorTest.
*/
public class UnevaluatedPropertiesValidatorTest {
/**
* Issue 962.
*/
@Test
void annotationsOnlyDroppedAtTheEndOfSchemaProcessing() {
String schemaData = "{\r\n"
+ " \"type\": \"object\",\r\n"
+ " \"required\": [\r\n"
+ " \"key1\"\r\n"
+ " ],\r\n"
+ " \"properties\": {\r\n"
+ " \"key1\": {\r\n"
+ " \"type\": \"string\"\r\n"
+ " },\r\n"
+ " \"key2\": {\r\n"
+ " \"type\": \"string\"\r\n"
+ " },\r\n"
+ " \"key3\": {\r\n"
+ " \"type\": \"string\"\r\n"
+ " }\r\n"
+ " },\r\n"
+ " \"unevaluatedProperties\": false\r\n"
+ "}";
String inputData = "{\r\n"
+ " \"key2\": \"value2\",\r\n"
+ " \"key3\": \"value3\",\r\n"
+ " \"key4\": \"value4\"\r\n"
+ "}";
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData);
Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON);
assertEquals(2, messages.size());
List<ValidationMessage> assertions = messages.stream().collect(Collectors.toList());
assertEquals("required", assertions.get(0).getType());
assertEquals("key1", assertions.get(0).getProperty());
assertEquals("unevaluatedProperties", assertions.get(1).getType());
assertEquals("key4", assertions.get(1).getProperty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@
},
"valid": false,
"validationMessages": [
"$.vehicle: property 'pontoons' must not be unevaluated",
"$.vehicle: property 'wings' must not be unevaluated"
"$.vehicle: must be valid to one and only one schema, but 2 are valid",
"$.vehicle: required property 'wheels' not found",
"$.vehicle: required property 'headlights' not found"
]
},
{
Expand All @@ -271,9 +272,10 @@
},
"valid": false,
"validationMessages": [
"$.vehicle: must be valid to one and only one schema, but 2 are valid",
"$.vehicle: property 'invalid' must not be unevaluated",
"$.vehicle: property 'pontoons' must not be unevaluated",
"$.vehicle: property 'wings' must not be unevaluated"
"$.vehicle: required property 'wheels' not found",
"$.vehicle: required property 'headlights' not found"
]
},
{
Expand All @@ -288,6 +290,7 @@
},
"valid": false,
"validationMessages": [
"$.vehicle: must be valid to one and only one schema, but 0 are valid",
"$.vehicle: property 'invalid' must not be unevaluated"
]
}
Expand Down

0 comments on commit c768bc1

Please sign in to comment.