Skip to content

Stops unevaluatedProperties and unevaluatedItems being applied recursively #827

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

Merged
merged 4 commits into from
Jun 28, 2023
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
36 changes: 11 additions & 25 deletions src/main/java/com/networknt/schema/UnevaluatedItemsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@

package com.networknt.schema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.utils.JsonNodeUtil;
import java.util.*;

public class UnevaluatedItemsValidator extends BaseJsonValidator {
private static final Logger logger = LoggerFactory.getLogger(UnevaluatedItemsValidator.class);
Expand All @@ -49,7 +41,7 @@ public UnevaluatedItemsValidator(String schemaPath, JsonNode schemaNode, JsonSch

@Override
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
if (this.disabled) return Collections.emptySet();
if (this.disabled || !node.isArray()) return Collections.emptySet();

debug(logger, node, rootNode, at);
CollectorContext collectorContext = CollectorContext.getInstance();
Expand Down Expand Up @@ -92,21 +84,15 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
}
}

private static final Pattern NUMERIC = Pattern.compile("^\\d+$");

private Set<String> allPaths(JsonNode node, String at) {
return JsonNodeUtil.allPaths(getPathType(), at, node)
.stream()
.filter(this::isArray)
.collect(Collectors.toSet());
}

private boolean isArray(String path) {
String jsonPointer = getPathType().convertToJsonPointer(path);
String[] segment = jsonPointer.split("/");
if (0 == segment.length) return false;
String lastSegment = segment[segment.length - 1];
return NUMERIC.matcher(lastSegment).matches();
PathType pathType = getPathType();
Set<String> collector = new HashSet<>();
int size = node.size();
for (int i = 0; i < size; ++i) {
String path = pathType.append(at, i);
collector.add(path);
}
return collector;
}

private Set<ValidationMessage> reportUnevaluatedPaths(Set<String> unevaluatedPaths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.utils.JsonNodeUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class UnevaluatedPropertiesValidator extends BaseJsonValidator {
private static final Logger logger = LoggerFactory.getLogger(UnevaluatedPropertiesValidator.class);
Expand All @@ -45,7 +41,7 @@ public UnevaluatedPropertiesValidator(String schemaPath, JsonNode schemaNode, Js

@Override
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
if (this.disabled) return Collections.emptySet();
if (this.disabled || !node.isObject()) return Collections.emptySet();

debug(logger, node, rootNode, at);
CollectorContext collectorContext = CollectorContext.getInstance();
Expand Down Expand Up @@ -88,21 +84,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
}
}

private static final Pattern NUMERIC = Pattern.compile("^\\d+$");

private Set<String> allPaths(JsonNode node, String at) {
return JsonNodeUtil.allPaths(getPathType(), at, node)
.stream()
.filter(this::isProperty)
.collect(Collectors.toSet());
}

private boolean isProperty(String path) {
String jsonPointer = getPathType().convertToJsonPointer(path);
String[] segment = jsonPointer.split("/");
if (0 == segment.length) return false;
String lastSegment = segment[segment.length - 1];
return !NUMERIC.matcher(lastSegment).matches();
PathType pathType = getPathType();
Set<String> collector = new HashSet<>();
node.fields().forEachRemaining(entry -> {
String path = pathType.append(at, entry.getKey());
collector.add(path);
});
return collector;
}

private Set<ValidationMessage> reportUnevaluatedPaths(Set<String> unevaluatedPaths) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/networknt/schema/UnevaluatedItemsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.networknt.schema;

import com.networknt.schema.SpecVersion.VersionFlag;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicNode;
import org.junit.jupiter.api.TestFactory;

import java.util.stream.Stream;

@DisplayName("Unevaluated Items")
public class UnevaluatedItemsTest extends AbstractJsonSchemaTestSuite {

@TestFactory
@DisplayName("Draft 2019-09")
Stream<DynamicNode> draft201909() {
return createTests(VersionFlag.V201909, "src/test/resources/schema/unevaluatedTests/unevaluated-items-tests.json");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"description": "unevaluatedItems should not affect sub-schemas",
"schema": {
"unevaluatedItems": {
"type": "object"
}
},
"tests": [
{
"description": "unevaluated item bar is in sub-schema",
"data": [
{
"foo": ["bar"]
}
],
"valid": true
}
]
}
]
82 changes: 62 additions & 20 deletions src/test/resources/schema/unevaluatedTests/unevaluated-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
"unevaluatedProperties": false
},
"residence": {
"flatNumber": {
"type": "string"
},
"flatName": {
"type": "string"
"properties": {
"flatNumber": {
"type": "string"
},
"flatName": {
"type": "string"
},
"landmark": {
"type": "string"
}
},
"landmark": {
"type": "string"
}
"unevaluatedProperties": false
}
},
"properties": {
Expand Down Expand Up @@ -202,10 +205,11 @@
}
}
}
]
}
},
"unevaluatedProperties": false
],
"unevaluatedProperties": false
},
"unevaluatedProperties": false
}
},
"tests": [
{
Expand All @@ -220,6 +224,22 @@
},
"valid": true
},
{
"description": "Data which satisfies 1 oneOf schemas, but fails due to unevaluated prop",
"data": {
"firstName": "First Name",
"age": 18,
"lastName": "Last Name",
"vehicle": {
"pontoons": "pontoons",
"wheels": "wheels"
}
},
"valid": false,
"validationMessages": [
"There are unevaluated properties at the following paths $.vehicle.wheels"
]
},
{
"description": "Data which satisfies 2 oneOf schemas",
"data": {
Expand Down Expand Up @@ -328,10 +348,11 @@
}
}
}
]
}
},
"unevaluatedProperties": false
],
"unevaluatedProperties": false
},
"unevaluatedProperties": false
}
},
"tests": [
{
Expand Down Expand Up @@ -453,10 +474,11 @@
}
}
}
]
}
},
"unevaluatedProperties": false
],
"unevaluatedProperties": false
},
"unevaluatedProperties": false
}
},
"tests": [
{
Expand Down Expand Up @@ -634,6 +656,26 @@
}
]
},
{
"description": "unevaluatedProperties should not affect sub-schemas",
"schema": {
"properties": {
"foo": {}
},
"unevaluatedProperties": false
},
"tests": [
{
"description": "unevaluated property bar is in sub-schema",
"data": {
"foo": {
"bar": "baz"
}
},
"valid": true
}
]
},
{
"description": "unevaluatedProperties true",
"schema": {
Expand Down