From e4cdb90a7516f06d43f767c674716d2ef6525aa0 Mon Sep 17 00:00:00 2001 From: dan-s1 Date: Mon, 1 May 2023 17:12:09 +0000 Subject: [PATCH] [NIFI-11509] Added configuration to allow for parsing comments in JSON. This closes #7220 Signed-off-by: Mike Thomsen --- .../nifi-standard-processors/pom.xml | 1 + .../nifi/processors/standard/ValidateJson.java | 7 ++++++- .../processors/standard/TestValidateJson.java | 16 ++++++++++++++++ .../simple-example-with-comments.json | 12 ++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestValidateJson/simple-example-with-comments.json diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml index d9aab6ce25..780b45dbf1 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml @@ -722,6 +722,7 @@ src/test/resources/TestValidateJson/schema-simple-example-unmatched-pattern.json src/test/resources/TestValidateJson/schema-simple-example.json src/test/resources/TestValidateJson/simple-example.json + src/test/resources/TestValidateJson/simple-example-with-comments.json diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ValidateJson.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ValidateJson.java index b28a40d007..5120b36700 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ValidateJson.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ValidateJson.java @@ -16,6 +16,7 @@ */ package org.apache.nifi.processors.standard; +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.networknt.schema.JsonSchema; @@ -151,7 +152,11 @@ public class ValidateJson extends AbstractProcessor { )) ); - private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final ObjectMapper MAPPER; + static { + MAPPER = new ObjectMapper(); + MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true); + } private JsonSchema schema; diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateJson.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateJson.java index 37b4f89408..14688ce706 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateJson.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateJson.java @@ -175,6 +175,22 @@ class TestValidateJson { assertThrows(AssertionFailedError.class, () -> runner.run()); } + @Test + void testJsonWithComments() { + final String schemaPath = getFilePath("schema-simple-example.json"); + runner.setProperty(ValidateJson.SCHEMA_CONTENT, schemaPath); + runner.setProperty(ValidateJson.SCHEMA_VERSION, SCHEMA_VERSION); + + runner.enqueue(getFileContent("simple-example-with-comments.json")); + + runner.run(); + + runner.assertTransferCount(ValidateJson.REL_FAILURE, 0); + runner.assertTransferCount(ValidateJson.REL_INVALID, 0); + runner.assertTransferCount(ValidateJson.REL_VALID, 1); + + assertValidationErrors(ValidateJson.REL_VALID, false); + } private void assertValidationErrors(Relationship relationship, boolean expected) { final Map attributes = runner.getFlowFilesForRelationship(relationship).get(0).getAttributes(); diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestValidateJson/simple-example-with-comments.json b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestValidateJson/simple-example-with-comments.json new file mode 100644 index 0000000000..c1bda16f51 --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestValidateJson/simple-example-with-comments.json @@ -0,0 +1,12 @@ +{ + //First comment + "FieldOne": "stringValue", + /*Second comment*/ + "FieldTwo": 1234, + //Third comment + "FieldThree": [ + { + "arrayField": "arrayValue" + } + ] +}