From 937105f24dde6defd8f467c9da839aacf36961f0 Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Wed, 7 Feb 2024 02:03:14 +0800 Subject: [PATCH] Fix patternProperties annotation (#955) --- .../schema/PatternPropertiesValidator.java | 5 +- .../PatternPropertiesValidatorTest.java | 47 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/networknt/schema/PatternPropertiesValidator.java b/src/main/java/com/networknt/schema/PatternPropertiesValidator.java index 1e5bc18c9..797558af0 100644 --- a/src/main/java/com/networknt/schema/PatternPropertiesValidator.java +++ b/src/main/java/com/networknt/schema/PatternPropertiesValidator.java @@ -86,7 +86,10 @@ public Set validate(ExecutionContext executionContext, JsonNo executionContext.getAnnotations() .put(JsonNodeAnnotation.builder().instanceLocation(instanceLocation) .evaluationPath(this.evaluationPath).schemaLocation(this.schemaLocation) - .keyword(getKeyword()).value(matchedInstancePropertyNames).build()); + .keyword(getKeyword()) + .value(matchedInstancePropertyNames != null ? matchedInstancePropertyNames + : Collections.emptySet()) + .build()); } return errors == null ? Collections.emptySet() : Collections.unmodifiableSet(errors); } diff --git a/src/test/java/com/networknt/schema/PatternPropertiesValidatorTest.java b/src/test/java/com/networknt/schema/PatternPropertiesValidatorTest.java index 39e7eb511..28c0e9eb1 100644 --- a/src/test/java/com/networknt/schema/PatternPropertiesValidatorTest.java +++ b/src/test/java/com/networknt/schema/PatternPropertiesValidatorTest.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.networknt.schema.SpecVersion.VersionFlag; +import com.networknt.schema.output.OutputUnit; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,7 +26,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.HashSet; import java.util.Set; /** @@ -108,7 +111,49 @@ void message() { assertEquals("999", message.getInstanceNode().toString()); assertEquals("/valid_array/0: integer found, string expected", message.getMessage()); assertNull(message.getProperty()); + } - + @SuppressWarnings("unchecked") + @Test + void annotation() { + String schemaData = "{\n" + + " \"$id\": \"https://www.example.org/schema\",\n" + + " \"type\": \"object\",\n" + + " \"patternProperties\": {\n" + + " \"^valid_\": {\n" + + " \"type\": [\"array\", \"string\"],\n" + + " \"items\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); + config.setPathType(PathType.JSON_POINTER); + JsonSchema schema = factory.getSchema(schemaData, config); + String inputData = "{\n" + + " \"test\": 5\n" + + "}"; + OutputUnit outputUnit = schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> { + executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true); + executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true); + }); + Set patternProperties = (Set) outputUnit.getAnnotations().get("patternProperties"); + assertTrue(patternProperties.isEmpty()); + + inputData = "{\n" + + " \"valid_array\": [\"999\", \"2\"],\n" + + " \"valid_string\": \"string_value\"" + + "}"; + outputUnit = schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> { + executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true); + executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true); + }); + patternProperties = (Set) outputUnit.getAnnotations().get("patternProperties"); + Set all = new HashSet<>(); + all.add("valid_array"); + all.add("valid_string"); + assertTrue(patternProperties.containsAll(patternProperties)); } }