Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unevaluatedProperties #963

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading