Repository: networknt/json-schema-validator
Pull request #583 (2022-06-11 11:15:54)

Fix unevaluatedPropeties with patternProperties and type union (#582)
Here is a fix proposition for issue: https://github.com/networknt/json-schema-validator/issues/582

Kind regards,
Kevan.


Patch:
diff --git a/src/main/java/com/networknt/schema/PatternPropertiesValidator.java b/src/main/java/com/networknt/schema/PatternPropertiesValidator.java
index 3d1f67a..641eadb 100644
--- a/src/main/java/com/networknt/schema/PatternPropertiesValidator.java
+++ b/src/main/java/com/networknt/schema/PatternPropertiesValidator.java
@@ -58,6 +58,7 @@ public class PatternPropertiesValidator extends BaseJsonValidator implements Jso
             for (Map.Entry<Pattern, JsonSchema> entry : schemas.entrySet()) {
                 Matcher m = entry.getKey().matcher(name);
                 if (m.find()) {
+                    addToEvaluatedProperties(at + "." + name);
                     errors.addAll(entry.getValue().validate(n, rootNode, at + "." + name));
                 }
             }
@@ -69,4 +70,16 @@ public class PatternPropertiesValidator extends BaseJsonValidator implements Jso
     public void preloadJsonSchema() {
         preloadJsonSchemas(schemas.values());
     }
+
+    private void addToEvaluatedProperties(String propertyPath) {
+        Object evaluatedProperties = CollectorContext.getInstance().get(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES);
+        List<String> evaluatedPropertiesList = null;
+        if (evaluatedProperties == null) {
+            evaluatedPropertiesList = new ArrayList<>();
+            CollectorContext.getInstance().add(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES, evaluatedPropertiesList);
+        } else {
+            evaluatedPropertiesList = (List<String>) evaluatedProperties;
+        }
+        evaluatedPropertiesList.add(propertyPath);
+    }
 }


Test patch:
diff --git a/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json b/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json
index 2519dcf..2a0f7e6 100644
--- a/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json
+++ b/src/test/resources/schema/unevaluatedTests/unevaluated-tests.json
@@ -2150,5 +2150,48 @@
         "valid": false
       }
     ]
+  },
+  {
+    "description": "unevaluatedProperties with patternProperties and type union",
+    "schema": {
+      "type": "object",
+      "patternProperties": {
+        "^valid_": {
+          "type": ["array", "string"],
+          "items": {
+            "type": "string"
+          }
+        }
+      },
+      "unevaluatedProperties": false
+    },
+    "tests": [
+      {
+        "description": "Not valid key against pattern",
+        "data": {
+          "valid_array": ["array1_value", "array2_value"],
+          "valid_string": "string_value",
+          "invalid_key": "this is an unevaluated properties due to key not matching the pattern"
+        },
+        "valid": false
+      },
+      {
+        "description": "Not valid type",
+        "data": {
+          "valid_array": ["array1_value", "array2_value"],
+          "valid_string": "string_value",
+          "valid_key": 5
+        },
+        "valid": false
+      },
+      {
+        "description": "Valid",
+        "data": {
+          "valid_array": ["array1_value", "array2_value"],
+          "valid_string": "string_value"
+        },
+        "valid": true
+      }
+    ]
   }
 ]
\ No newline at end of file