[NIFI-11509] Added configuration to allow for parsing comments in JSON.

This closes #7220

Signed-off-by: Mike Thomsen <mthomsen@apache.org>
This commit is contained in:
dan-s1 2023-05-01 17:12:09 +00:00 committed by Mike Thomsen
parent 71c7d38c75
commit e4cdb90a75
4 changed files with 35 additions and 1 deletions

View File

@ -722,6 +722,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>

View File

@ -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;

View File

@ -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();

View File

@ -0,0 +1,12 @@
{
//First comment
"FieldOne": "stringValue",
/*Second comment*/
"FieldTwo": 1234,
//Third comment
"FieldThree": [
{
"arrayField": "arrayValue"
}
]
}