Skip to content

Commit 45d2358

Browse files
authored
fix for #461 (#462)
* fix for #461 simple implementation (aligned with one-of) * fix for #461 simple implementation * simplify walker as test does not depend on its logic
1 parent 9419ce7 commit 45d2358

File tree

5 files changed

+144
-11
lines changed

5 files changed

+144
-11
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.slf4j.LoggerFactory;
2222

2323
import java.util.*;
24+
import java.util.stream.Collectors;
2425

2526
public class AnyOfValidator extends BaseJsonValidator implements JsonValidator {
2627
private static final Logger logger = LoggerFactory.getLogger(RequiredValidator.class);
@@ -100,13 +101,21 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
100101

101102
@Override
102103
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
103-
Set<ValidationMessage> validationMessages = new LinkedHashSet<ValidationMessage>();
104-
104+
ArrayList<Set<ValidationMessage>> results = new ArrayList<>(schemas.size());
105105
for (JsonSchema schema : schemas) {
106-
// Walk through the schema
107-
validationMessages.addAll(schema.walk(node, rootNode, at, shouldValidateSchema));
106+
results.add(schema.walk(node, rootNode, at, shouldValidateSchema));
107+
}
108+
if(! shouldValidateSchema) {
109+
return new LinkedHashSet<>();
110+
}
111+
boolean atLeastOneValid = results.stream()
112+
.anyMatch(Set::isEmpty);
113+
if(atLeastOneValid) {
114+
return new LinkedHashSet<>();
108115
}
109-
return Collections.unmodifiableSet(validationMessages);
116+
return results.stream()
117+
.flatMap(Collection::stream)
118+
.collect(Collectors.toCollection(LinkedHashSet::new));
110119
}
111120

112121
@Override

src/test/java/com/networknt/schema/Issue451Test.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.networknt.schema.walk.JsonSchemaWalkListener;
66
import com.networknt.schema.walk.WalkEvent;
77
import com.networknt.schema.walk.WalkFlow;
8+
import org.junit.jupiter.api.AfterEach;
89
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Disabled;
911
import org.junit.jupiter.api.Test;
1012

1113
import java.io.InputStream;
@@ -17,6 +19,9 @@
1719
* Validating anyOf walker
1820
*/
1921
public class Issue451Test {
22+
23+
private static final String COLLECTOR_ID = "collector-451";
24+
2025
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
2126
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
2227
SchemaValidatorsConfig svc = new SchemaValidatorsConfig();
@@ -29,20 +34,46 @@ protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exce
2934
return mapper.readTree(content);
3035
}
3136

32-
@SuppressWarnings("unchecked")
37+
@AfterEach
38+
public void cleanup() {
39+
if (CollectorContext.getInstance() != null) {
40+
CollectorContext.getInstance().reset();
41+
}
42+
}
43+
44+
@Test
45+
public void shouldWalkAnyOfProperties() {
46+
walk(null, false);
47+
}
48+
49+
@Test
50+
public void shouldWalkAnyOfPropertiesWithWithPayloadAndValidation() throws Exception {
51+
JsonNode data = getJsonNodeFromStreamContent(Issue451Test.class.getResourceAsStream(
52+
"/data/issue451.json"));
53+
walk(data,true);
54+
}
55+
3356
@Test
34-
public void shouldWalkAnyOfProperties() throws Exception {
57+
public void shouldWalkAnyOfPropertiesWithWithPayload() throws Exception {
58+
JsonNode data = getJsonNodeFromStreamContent(Issue451Test.class.getResourceAsStream(
59+
"/data/issue451.json"));
60+
walk(data, false);
61+
}
62+
63+
@SuppressWarnings("unchecked")
64+
private void walk(JsonNode data, boolean shouldValidate) {
3565
String schemaPath = "/schema/issue451-v7.json";
3666
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
3767
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
3868

39-
schema.walk(null, false);
69+
schema.walk(data, shouldValidate);
4070

41-
Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451");
71+
Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get(COLLECTOR_ID);
4272
Assertions.assertEquals(2, collector.get("#/definitions/definition1/properties/a"));
4373
Assertions.assertEquals(2, collector.get("#/definitions/definition2/properties/x"));
4474
}
4575

76+
4677
private static class CountingWalker implements JsonSchemaWalkListener {
4778
@Override
4879
public WalkFlow onWalkStart(WalkEvent walkEvent) {
@@ -57,10 +88,10 @@ public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMess
5788
}
5889

5990
private Map<String, Integer> collector() {
60-
Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451");
91+
Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get(COLLECTOR_ID);
6192
if(collector == null) {
6293
collector = new HashMap<>();
63-
CollectorContext.getInstance().add("collector-451", collector);
94+
CollectorContext.getInstance().add(COLLECTOR_ID, collector);
6495
}
6596

6697
return collector;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.networknt.schema.walk.JsonSchemaWalkListener;
6+
import com.networknt.schema.walk.WalkEvent;
7+
import com.networknt.schema.walk.WalkFlow;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.io.IOException;
12+
import java.net.URI;
13+
import java.net.URISyntaxException;
14+
import java.util.Set;
15+
16+
public class Issue461Test {
17+
protected ObjectMapper mapper = new ObjectMapper();
18+
19+
protected JsonSchema getJsonSchemaFromStreamContentV7(URI schemaUri) {
20+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
21+
SchemaValidatorsConfig svc = new SchemaValidatorsConfig();
22+
svc.addKeywordWalkListener(ValidatorTypeCode.PROPERTIES.getValue(), new Walker());
23+
return factory.getSchema(schemaUri, svc);
24+
}
25+
26+
@Test
27+
public void shouldWalkWithValidation() throws URISyntaxException, IOException {
28+
JsonSchema schema = getJsonSchemaFromStreamContentV7(new URI("http://json-schema" +
29+
".org/draft-07/schema#"));
30+
JsonNode data = mapper.readTree(Issue461Test.class.getResource("/data/issue461-v7.json"));
31+
ValidationResult result = schema.walk(data, true);
32+
Assertions.assertTrue(result.getValidationMessages().isEmpty());
33+
}
34+
35+
/**
36+
* Example NOP walker
37+
*/
38+
private static class Walker implements JsonSchemaWalkListener {
39+
@Override
40+
public WalkFlow onWalkStart(final WalkEvent walkEvent) {
41+
return WalkFlow.CONTINUE;
42+
}
43+
44+
@Override
45+
public void onWalkEnd(final WalkEvent walkEvent,
46+
final Set<ValidationMessage> validationMessages) {
47+
}
48+
}
49+
}

src/test/resources/data/issue451.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"allOfAttr" : {
3+
"a": "foo",
4+
"x": 1,
5+
"y": "xx"
6+
},
7+
"anyOfAttr" : {
8+
"a": "bar",
9+
"x": 1
10+
}
11+
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"properties": {
5+
"firstName": {
6+
"type": "string"
7+
},
8+
"lastName": {
9+
"type": "string"
10+
},
11+
"address": {
12+
"type": "object",
13+
"properties": {
14+
"street": {
15+
"type": "string"
16+
},
17+
"city": {
18+
"type": "string"
19+
}
20+
},
21+
"required": [
22+
"street",
23+
"city"
24+
]
25+
}
26+
},
27+
"required": [
28+
"firstName",
29+
"lastName",
30+
"address"
31+
]
32+
}

0 commit comments

Comments
 (0)