This is an automated email from the ASF dual-hosted git repository.
mthomsen pushed a commit to branch support/nifi-1.x
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/support/nifi-1.x by this push:
new 2e2604a901 [NIFI-11509] Added configuration to allow for parsing
comments in JSON.
2e2604a901 is described below
commit 2e2604a9019893888ea7cb893c1b08c4b3402b91
Author: dan-s1 <[email protected]>
AuthorDate: Mon May 1 17:12:09 2023 +0000
[NIFI-11509] Added configuration to allow for parsing comments in JSON.
This closes #7220
Signed-off-by: Mike Thomsen <[email protected]>
---
.../nifi-standard-processors/pom.xml | 1 +
.../apache/nifi/processors/standard/ValidateJson.java | 7 ++++++-
.../nifi/processors/standard/TestValidateJson.java | 16 ++++++++++++++++
.../TestValidateJson/simple-example-with-comments.json | 12 ++++++++++++
4 files changed, 35 insertions(+), 1 deletion(-)
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 d9ab2bb669..4822d9696e 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
@@ -732,6 +732,7 @@
<exclude>src/test/resources/TestValidateJson/schema-simple-example-unmatched-pattern.json</exclude>
<exclude>src/test/resources/TestValidateJson/schema-simple-example.json</exclude>
<exclude>src/test/resources/TestValidateJson/simple-example.json</exclude>
+
<exclude>src/test/resources/TestValidateJson/simple-example-with-comments.json</exclude>
</excludes>
</configuration>
</plugin>
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<String, String> 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"
+ }
+ ]
+}