Skip to content

Simplifies how evaluated properties and array items are tracked #790

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 7 commits into from
Jun 5, 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
28 changes: 5 additions & 23 deletions src/main/java/com/networknt/schema/AllOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.networknt.schema.CollectorContext.Scope;

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

Expand Down Expand Up @@ -47,21 +49,11 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String

Set<ValidationMessage> childSchemaErrors = new LinkedHashSet<>();

Collection<String> newEvaluatedItems = Collections.emptyList();
Collection<String> newEvaluatedProperties = Collections.emptyList();

for (JsonSchema schema : this.schemas) {
// As AllOf might contain multiple schemas take a backup of evaluated stuff.
Collection<String> backupEvaluatedItems = collectorContext.getEvaluatedItems();
Collection<String> backupEvaluatedProperties = collectorContext.getEvaluatedProperties();

Set<ValidationMessage> localErrors = new HashSet<>();

Scope parentScope = collectorContext.enterDynamicScope();
try {
// Make the evaluated lists empty.
collectorContext.resetEvaluatedItems();
collectorContext.resetEvaluatedProperties();

if (!state.isWalkEnabled()) {
localErrors = schema.validate(node, rootNode, at);
} else {
Expand All @@ -70,12 +62,6 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String

childSchemaErrors.addAll(localErrors);

// Keep Collecting total evaluated properties.
if (localErrors.isEmpty()) {
newEvaluatedItems = collectorContext.getEvaluatedItems();
newEvaluatedProperties = collectorContext.getEvaluatedProperties();
}

if (this.validationContext.getConfig().isOpenAPI3StyleDiscriminators()) {
final Iterator<JsonNode> arrayElements = this.schemaNode.elements();
while (arrayElements.hasNext()) {
Expand Down Expand Up @@ -108,14 +94,10 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
}
}
} finally {
collectorContext.setEvaluatedItems(backupEvaluatedItems);
collectorContext.setEvaluatedProperties(backupEvaluatedProperties);
Scope scope = collectorContext.exitDynamicScope();
if (localErrors.isEmpty()) {
collectorContext.getEvaluatedItems().addAll(newEvaluatedItems);
collectorContext.getEvaluatedProperties().addAll(newEvaluatedProperties);
parentScope.mergeWith(scope);
}
newEvaluatedItems = Collections.emptyList();
newEvaluatedProperties = Collections.emptyList();
}
}

Expand Down
108 changes: 54 additions & 54 deletions src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.CollectorContext.Scope;

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

Expand Down Expand Up @@ -61,64 +63,64 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String

Set<ValidationMessage> allErrors = new LinkedHashSet<>();

// As anyOf might contain multiple schemas take a backup of evaluated stuff.
Collection<String> backupEvaluatedItems = collectorContext.getEvaluatedItems();
Collection<String> backupEvaluatedProperties = collectorContext.getEvaluatedProperties();

// Make the evaluated lists empty.
collectorContext.resetEvaluatedItems();
collectorContext.resetEvaluatedProperties();

Scope grandParentScope = collectorContext.enterDynamicScope();
try {
int numberOfValidSubSchemas = 0;
for (int i = 0; i < this.schemas.size(); ++i) {
JsonSchema schema = this.schemas.get(i);
state.setMatchedNode(initialHasMatchedNode);
Set<ValidationMessage> errors;

if (schema.hasTypeValidator()) {
TypeValidator typeValidator = schema.getTypeValidator();
//If schema has type validator and node type doesn't match with schemaType then ignore it
//For union type, it is a must to call TypeValidator
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {
allErrors.add(buildValidationMessage(at, typeValidator.getSchemaType().toString()));
continue;
for (JsonSchema schema: this.schemas) {
Set<ValidationMessage> errors = Collections.emptySet();
Scope parentScope = collectorContext.enterDynamicScope();
try {
state.setMatchedNode(initialHasMatchedNode);

if (schema.hasTypeValidator()) {
TypeValidator typeValidator = schema.getTypeValidator();
//If schema has type validator and node type doesn't match with schemaType then ignore it
//For union type, it is a must to call TypeValidator
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {
allErrors.add(buildValidationMessage(at, typeValidator.getSchemaType().toString()));
continue;
}
}
}
if (!state.isWalkEnabled()) {
errors = schema.validate(node, rootNode, at);
} else {
errors = schema.walk(node, rootNode, at, true);
}

// check if any validation errors have occurred
if (errors.isEmpty()) {
// check whether there are no errors HOWEVER we have validated the exact validator
if (!state.hasMatchedNode()) {
continue;
if (!state.isWalkEnabled()) {
errors = schema.validate(node, rootNode, at);
} else {
errors = schema.walk(node, rootNode, at, true);
}
// we found a valid subschema, so increase counter
numberOfValidSubSchemas++;
}

if (errors.isEmpty() && (!this.validationContext.getConfig().isOpenAPI3StyleDiscriminators())) {
// Clear all errors.
allErrors.clear();
// return empty errors.
return errors;
} else if (this.validationContext.getConfig().isOpenAPI3StyleDiscriminators()) {
if (this.discriminatorContext.isDiscriminatorMatchFound()) {
if (!errors.isEmpty()) {
errors.add(buildValidationMessage(at, DISCRIMINATOR_REMARK));
allErrors.addAll(errors);
} else {
// Clear all errors.
allErrors.clear();
// check if any validation errors have occurred
if (errors.isEmpty()) {
// check whether there are no errors HOWEVER we have validated the exact validator
if (!state.hasMatchedNode()) {
continue;
}
// we found a valid subschema, so increase counter
numberOfValidSubSchemas++;
}

if (errors.isEmpty() && (!this.validationContext.getConfig().isOpenAPI3StyleDiscriminators())) {
// Clear all errors.
allErrors.clear();
// return empty errors.
return errors;
} else if (this.validationContext.getConfig().isOpenAPI3StyleDiscriminators()) {
if (this.discriminatorContext.isDiscriminatorMatchFound()) {
if (!errors.isEmpty()) {
allErrors.addAll(errors);
allErrors.add(buildValidationMessage(at, DISCRIMINATOR_REMARK));
} else {
// Clear all errors.
allErrors.clear();
}
return errors;
}
}
allErrors.addAll(errors);
} finally {
Scope scope = collectorContext.exitDynamicScope();
if (errors.isEmpty()) {
parentScope.mergeWith(scope);
}
}
allErrors.addAll(errors);
}

// determine only those errors which are NOT of type "required" property missing
Expand All @@ -138,14 +140,12 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
if (this.validationContext.getConfig().isOpenAPI3StyleDiscriminators()) {
this.validationContext.leaveDiscriminatorContextImmediately(at);
}

Scope parentScope = collectorContext.exitDynamicScope();
if (allErrors.isEmpty()) {
state.setMatchedNode(true);
} else {
collectorContext.getEvaluatedItems().clear();
collectorContext.getEvaluatedProperties().clear();
grandParentScope.mergeWith(parentScope);
}
collectorContext.getEvaluatedItems().addAll(backupEvaluatedItems);
collectorContext.getEvaluatedProperties().addAll(backupEvaluatedProperties);
}
return Collections.unmodifiableSet(allErrors);
}
Expand Down
Loading