Skip to content

Commit 82aafd9

Browse files
authored
fix for #451 (#452)
1 parent 7522176 commit 82aafd9

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<modelVersion>4.0.0</modelVersion>
2121
<groupId>com.networknt</groupId>
2222
<artifactId>json-schema-validator</artifactId>
23-
<version>1.0.59</version>
23+
<version>1.0.60</version>
2424
<packaging>bundle</packaging>
2525
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
2626
<url>https://github.com/networknt/json-schema-validator</url>

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

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
9898
return Collections.unmodifiableSet(allErrors);
9999
}
100100

101+
@Override
102+
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
103+
Set<ValidationMessage> validationMessages = new LinkedHashSet<ValidationMessage>();
104+
105+
for (JsonSchema schema : schemas) {
106+
// Walk through the schema
107+
validationMessages.addAll(schema.walk(node, rootNode, at, shouldValidateSchema));
108+
}
109+
return Collections.unmodifiableSet(validationMessages);
110+
}
111+
101112
@Override
102113
public void preloadJsonSchema() {
103114
preloadJsonSchemas(schemas);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.InputStream;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.Set;
15+
16+
/**
17+
* Validating anyOf walker
18+
*/
19+
public class Issue451Test {
20+
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
21+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
22+
SchemaValidatorsConfig svc = new SchemaValidatorsConfig();
23+
svc.addPropertyWalkListener(new CountingWalker());
24+
return factory.getSchema(schemaContent, svc);
25+
}
26+
27+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
28+
ObjectMapper mapper = new ObjectMapper();
29+
return mapper.readTree(content);
30+
}
31+
32+
@SuppressWarnings("unchecked")
33+
@Test
34+
public void shouldWalkAnyOfProperties() throws Exception {
35+
String schemaPath = "/schema/issue451-v7.json";
36+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
37+
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
38+
39+
schema.walk(null, false);
40+
41+
Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451");
42+
Assertions.assertEquals(2, collector.get("#/definitions/definition1/properties/a"));
43+
Assertions.assertEquals(2, collector.get("#/definitions/definition2/properties/x"));
44+
}
45+
46+
private static class CountingWalker implements JsonSchemaWalkListener {
47+
@Override
48+
public WalkFlow onWalkStart(WalkEvent walkEvent) {
49+
String path = walkEvent.getSchemaPath();
50+
collector().compute(path, (k, v) -> v == null ? 1 : v + 1);
51+
return WalkFlow.CONTINUE;
52+
}
53+
54+
@Override
55+
public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMessages) {
56+
57+
}
58+
59+
private Map<String, Integer> collector() {
60+
Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451");
61+
if(collector == null) {
62+
collector = new HashMap<>();
63+
CollectorContext.getInstance().add("collector-451", collector);
64+
}
65+
66+
return collector;
67+
}
68+
}
69+
}
70+
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://example.com/issue-451.json",
4+
"title": "AllOf structuring payload",
5+
"description": "Test description",
6+
"type": "object",
7+
"properties": {
8+
"allOfAttr" : {
9+
"allOf" : [
10+
{"$ref": "#/definitions/definition1"},
11+
{"$ref": "#/definitions/definition2"}
12+
]
13+
},
14+
"anyOfAttr" : {
15+
"anyOf" : [
16+
{"$ref": "#/definitions/definition1"},
17+
{"$ref": "#/definitions/definition2"}
18+
]
19+
}
20+
},
21+
"additionalProperties": false,
22+
"definitions": {
23+
"definition1" : {
24+
"type": "object",
25+
"properties": {
26+
"a" : {"type": "string"},
27+
"c" : {"type": "integer"}
28+
},
29+
"additionalProperties": false
30+
},
31+
"definition2" : {
32+
"type": "object",
33+
"properties": {
34+
"x" : {"type": "number"},
35+
"y" : {"type": "number"}
36+
},
37+
"required": ["x", "y"],
38+
"additionalProperties": false
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)