diff --git a/src/main/java/com/networknt/schema/JsonSchema.java b/src/main/java/com/networknt/schema/JsonSchema.java index 44b513971..1171366ef 100644 --- a/src/main/java/com/networknt/schema/JsonSchema.java +++ b/src/main/java/com/networknt/schema/JsonSchema.java @@ -557,7 +557,6 @@ public Set 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<>(); } @@ -595,6 +594,11 @@ public Set 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; } diff --git a/src/main/java/com/networknt/schema/result/JsonNodeResults.java b/src/main/java/com/networknt/schema/result/JsonNodeResults.java index 07a419441..78b51f3bd 100644 --- a/src/main/java/com/networknt/schema/result/JsonNodeResults.java +++ b/src/main/java/com/networknt/schema/result/JsonNodeResults.java @@ -44,7 +44,7 @@ public boolean isValid(JsonNodePath instanceLocation, JsonNodePath evaluationPat List 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; } diff --git a/src/test/java/com/networknt/schema/UnevaluatedPropertiesValidatorTest.java b/src/test/java/com/networknt/schema/UnevaluatedPropertiesValidatorTest.java new file mode 100644 index 000000000..eee772886 --- /dev/null +++ b/src/test/java/com/networknt/schema/UnevaluatedPropertiesValidatorTest.java @@ -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 messages = schema.validate(inputData, InputFormat.JSON); + assertEquals(2, messages.size()); + List 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()); + } +} diff --git a/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json b/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json index fdd7dc148..a0e19fa71 100644 --- a/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json +++ b/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json @@ -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" ] }, { @@ -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" ] }, { @@ -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" ] }