Repository: networknt/json-schema-validator
Pull request #452 (2021-09-22 07:53:48)

Fix proposal for #451


Patch:
diff --git a/pom.xml b/pom.xml
index 5a54bc5..f4bf9bf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,7 +20,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.networknt</groupId>
     <artifactId>json-schema-validator</artifactId>
-    <version>1.0.59</version>
+    <version>1.0.60</version>
     <packaging>bundle</packaging>
     <description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
     <url>https://github.com/networknt/json-schema-validator</url>
diff --git a/src/main/java/com/networknt/schema/AnyOfValidator.java b/src/main/java/com/networknt/schema/AnyOfValidator.java
index 15edf5c..d93796c 100644
--- a/src/main/java/com/networknt/schema/AnyOfValidator.java
+++ b/src/main/java/com/networknt/schema/AnyOfValidator.java
@@ -98,6 +98,17 @@ public class AnyOfValidator extends BaseJsonValidator implements JsonValidator {
         return Collections.unmodifiableSet(allErrors);
     }
 
+    @Override
+    public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
+        Set<ValidationMessage> validationMessages = new LinkedHashSet<ValidationMessage>();
+
+        for (JsonSchema schema : schemas) {
+            // Walk through the schema
+            validationMessages.addAll(schema.walk(node, rootNode, at, shouldValidateSchema));
+        }
+        return Collections.unmodifiableSet(validationMessages);
+    }
+
     @Override
     public void preloadJsonSchema() {
         preloadJsonSchemas(schemas);


Test patch:
diff --git a/src/test/java/com/networknt/schema/Issue451Test.java b/src/test/java/com/networknt/schema/Issue451Test.java
new file mode 100644
index 0000000..cf73563
--- /dev/null
+++ b/src/test/java/com/networknt/schema/Issue451Test.java
@@ -0,0 +1,70 @@
+package com.networknt.schema;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.networknt.schema.walk.JsonSchemaWalkListener;
+import com.networknt.schema.walk.WalkEvent;
+import com.networknt.schema.walk.WalkFlow;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Validating anyOf walker
+ */
+public class Issue451Test {
+    protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
+        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
+        SchemaValidatorsConfig svc = new SchemaValidatorsConfig();
+        svc.addPropertyWalkListener(new CountingWalker());
+        return factory.getSchema(schemaContent, svc);
+    }
+
+    protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
+        ObjectMapper mapper = new ObjectMapper();
+        return mapper.readTree(content);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void shouldWalkAnyOfProperties() throws Exception {
+        String schemaPath = "/schema/issue451-v7.json";
+        InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
+        JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
+
+        schema.walk(null, false);
+
+        Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451");
+        Assertions.assertEquals(2, collector.get("#/definitions/definition1/properties/a"));
+        Assertions.assertEquals(2, collector.get("#/definitions/definition2/properties/x"));
+    }
+
+    private static class CountingWalker implements JsonSchemaWalkListener {
+        @Override
+        public WalkFlow onWalkStart(WalkEvent walkEvent) {
+            String path = walkEvent.getSchemaPath();
+            collector().compute(path, (k, v) -> v == null ? 1 : v + 1);
+            return WalkFlow.CONTINUE;
+        }
+
+        @Override
+        public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMessages) {
+
+        }
+
+        private Map<String, Integer> collector() {
+            Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451");
+            if(collector == null) {
+                collector = new HashMap<>();
+                CollectorContext.getInstance().add("collector-451", collector);
+            }
+
+            return collector;
+        }
+    }
+}
+
diff --git a/src/test/resources/schema/issue451-v7.json b/src/test/resources/schema/issue451-v7.json
new file mode 100644
index 0000000..de37523
--- /dev/null
+++ b/src/test/resources/schema/issue451-v7.json
@@ -0,0 +1,41 @@
+{
+  "$schema": "http://json-schema.org/draft-07/schema#",
+  "$id": "https://example.com/issue-451.json",
+  "title": "AllOf structuring payload",
+  "description": "Test description",
+  "type": "object",
+  "properties": {
+    "allOfAttr" : {
+      "allOf" : [
+        {"$ref":  "#/definitions/definition1"},
+        {"$ref":  "#/definitions/definition2"}
+      ]
+    },
+    "anyOfAttr" : {
+      "anyOf" : [
+        {"$ref":  "#/definitions/definition1"},
+        {"$ref":  "#/definitions/definition2"}
+      ]
+    }
+  },
+  "additionalProperties": false,
+  "definitions": {
+    "definition1" : {
+      "type": "object",
+      "properties": {
+        "a" : {"type": "string"},
+        "c" : {"type": "integer"}
+      },
+      "additionalProperties": false
+    },
+    "definition2" : {
+      "type": "object",
+      "properties": {
+        "x" : {"type": "number"},
+        "y" : {"type": "number"}
+      },
+      "required": ["x", "y"],
+      "additionalProperties": false
+    }
+  }
+}
\ No newline at end of file