Skip to content

Commit

Permalink
Issue 606: Handle matched state in AnyOfValidator (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgerke-1L authored Sep 7, 2022
1 parent 1c69c01 commit cbc03f8
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
validationContext.enterDiscriminatorContext(this.discriminatorContext, at);
}

boolean initialHasMatchedNode = state.hasMatchedNode();

Set<ValidationMessage> allErrors = new LinkedHashSet<ValidationMessage>();
String typeValidatorName = "anyOf/type";

Expand All @@ -72,6 +74,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
try {
int numberOfValidSubSchemas = 0;
for (JsonSchema schema : schemas) {
state.setMatchedNode(initialHasMatchedNode);
Set<ValidationMessage> errors = new HashSet<>();
if (schema.getValidators().containsKey(typeValidatorName)) {
TypeValidator typeValidator = ((TypeValidator) schema.getValidators().get(typeValidatorName));
Expand All @@ -87,7 +90,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
} 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
Expand All @@ -96,8 +99,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
}
// we found a valid subschema, so increase counter
numberOfValidSubSchemas++;
}
}

if (errors.isEmpty() && (!this.validationContext.getConfig().isOpenAPI3StyleDiscriminators())) {
// Clear all errors.
allErrors.clear();
Expand Down Expand Up @@ -137,6 +140,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
}
if (allErrors.isEmpty()) {
addEvaluatedProperties(backupEvaluatedProperties);
state.setMatchedNode(true);
} else {
CollectorContext.getInstance().add(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES, backupEvaluatedProperties);
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/networknt/schema/Issue606Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.util.Set;

public class Issue606Test {
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
return factory.getSchema(schemaContent);
}

protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(content);
return node;
}

@Test
public void shouldWorkV7() throws Exception {
String schemaPath = "/schema/issue606-v7.json";
String dataPath = "/data/issue606.json";
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
Set<ValidationMessage> errors = schema.validate(node);
Assertions.assertEquals(0, errors.size());
}
}
8 changes: 8 additions & 0 deletions src/test/resources/data/issue606.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"V": [
{
"A": "foo",
"B": "bar"
}
]
}
73 changes: 73 additions & 0 deletions src/test/resources/schema/issue606-v7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"V": {
"type": "array",
"items": {
"oneOf": [
{
"type": "object",
"properties": {
"X": {
"type": "string"
}
},
"required": [
"X"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"A": {
"type": "string"
},
"B": {
"type": "string"
},
"C": {
"type": "string"
}
},
"anyOf": [
{
"properties": {
"origin": {
"const": "not-present"
}
},
"required": [
"A",
"C"
]
},
{
"properties": {
"origin": {
"const": "not-present-either"
}
},
"required": [
"A",
"B"
]
}
],
"additionalProperties": false,
"required": [
"A"
],
"not": {
"type": "object",
"required": [
"X"
]
}
}
]
}
}
}
}

0 comments on commit cbc03f8

Please sign in to comment.