Skip to content

Commit 4fb2949

Browse files
committed
Issue 606: Handle matched state in AnyOfValidator
1 parent 1c69c01 commit 4fb2949

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

src/main/java/com/networknt/schema/AnyOfValidator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6060
validationContext.enterDiscriminatorContext(this.discriminatorContext, at);
6161
}
6262

63+
boolean initialHasMatchedNode = state.hasMatchedNode();
64+
6365
Set<ValidationMessage> allErrors = new LinkedHashSet<ValidationMessage>();
6466
String typeValidatorName = "anyOf/type";
6567

@@ -72,6 +74,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
7274
try {
7375
int numberOfValidSubSchemas = 0;
7476
for (JsonSchema schema : schemas) {
77+
state.setMatchedNode(initialHasMatchedNode);
7578
Set<ValidationMessage> errors = new HashSet<>();
7679
if (schema.getValidators().containsKey(typeValidatorName)) {
7780
TypeValidator typeValidator = ((TypeValidator) schema.getValidators().get(typeValidatorName));
@@ -87,7 +90,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
8790
} else {
8891
errors = schema.walk(node, rootNode, at, true);
8992
}
90-
93+
9194
// check if any validation errors have occurred
9295
if (errors.isEmpty()) {
9396
// check whether there are no errors HOWEVER we have validated the exact validator
@@ -96,8 +99,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
9699
}
97100
// we found a valid subschema, so increase counter
98101
numberOfValidSubSchemas++;
99-
}
100-
102+
}
103+
101104
if (errors.isEmpty() && (!this.validationContext.getConfig().isOpenAPI3StyleDiscriminators())) {
102105
// Clear all errors.
103106
allErrors.clear();
@@ -137,6 +140,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
137140
}
138141
if (allErrors.isEmpty()) {
139142
addEvaluatedProperties(backupEvaluatedProperties);
143+
state.setMatchedNode(true);
140144
} else {
141145
CollectorContext.getInstance().add(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES, backupEvaluatedProperties);
142146
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.InputStream;
9+
import java.util.Set;
10+
11+
public class Issue606Test {
12+
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
13+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
14+
return factory.getSchema(schemaContent);
15+
}
16+
17+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
18+
ObjectMapper mapper = new ObjectMapper();
19+
JsonNode node = mapper.readTree(content);
20+
return node;
21+
}
22+
23+
@Test
24+
public void shouldWorkV7() throws Exception {
25+
String schemaPath = "/schema/issue606-v7.json";
26+
String dataPath = "/data/issue606.json";
27+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
28+
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
29+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
30+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
31+
Set<ValidationMessage> errors = schema.validate(node);
32+
Assertions.assertEquals(0, errors.size());
33+
}
34+
}

src/test/resources/data/issue606.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"V": [
3+
{
4+
"A": "foo",
5+
"B": "bar"
6+
}
7+
]
8+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"properties": {
5+
"V": {
6+
"type": "array",
7+
"items": {
8+
"oneOf": [
9+
{
10+
"type": "object",
11+
"properties": {
12+
"X": {
13+
"type": "string"
14+
}
15+
},
16+
"required": [
17+
"X"
18+
],
19+
"additionalProperties": false
20+
},
21+
{
22+
"type": "object",
23+
"properties": {
24+
"A": {
25+
"type": "string"
26+
},
27+
"B": {
28+
"type": "string"
29+
},
30+
"C": {
31+
"type": "string"
32+
}
33+
},
34+
"anyOf": [
35+
{
36+
"properties": {
37+
"origin": {
38+
"const": "not-present"
39+
}
40+
},
41+
"required": [
42+
"A",
43+
"C"
44+
]
45+
},
46+
{
47+
"properties": {
48+
"origin": {
49+
"const": "not-present-either"
50+
}
51+
},
52+
"required": [
53+
"A",
54+
"B"
55+
]
56+
}
57+
],
58+
"additionalProperties": false,
59+
"required": [
60+
"A"
61+
],
62+
"not": {
63+
"type": "object",
64+
"required": [
65+
"X"
66+
]
67+
}
68+
}
69+
]
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)