From 63bda32a8befe7f0da01a6c543462f6b2b2daced Mon Sep 17 00:00:00 2001 From: Matt Burgess Date: Tue, 8 Nov 2016 11:56:14 -0500 Subject: [PATCH] NIFI-2998: Add validator to Avro Record Name in InferAvroSchema This closes #1187 --- .../nifi/processors/kite/InferAvroSchema.java | 7 ++++-- .../processors/kite/TestInferAvroSchema.java | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/main/java/org/apache/nifi/processors/kite/InferAvroSchema.java b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/main/java/org/apache/nifi/processors/kite/InferAvroSchema.java index aad48ae02d..0edbd2b17e 100644 --- a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/main/java/org/apache/nifi/processors/kite/InferAvroSchema.java +++ b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/main/java/org/apache/nifi/processors/kite/InferAvroSchema.java @@ -57,6 +57,7 @@ import java.util.Set; import java.util.HashSet; import java.util.Collections; import java.util.concurrent.atomic.AtomicReference; +import java.util.regex.Pattern; @Tags({"kite", "avro", "infer", "schema", "csv", "json"}) @InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) @@ -111,6 +112,7 @@ public class InferAvroSchema public static final String CSV_MIME_TYPE = "text/csv"; public static final String AVRO_MIME_TYPE = "application/avro-binary"; public static final String AVRO_FILE_EXTENSION = ".avro"; + public static final Pattern AVRO_RECORD_NAME_PATTERN = Pattern.compile("[A-Za-z_]+[A-Za-z0-9_.]*[^.]"); public static final PropertyDescriptor SCHEMA_DESTINATION = new PropertyDescriptor.Builder() .name("Schema Output Destination") @@ -202,10 +204,11 @@ public class InferAvroSchema public static final PropertyDescriptor RECORD_NAME = new PropertyDescriptor.Builder() .name("Avro Record Name") - .description("Value to be placed in the Avro record schema \"name\" field.") + .description("Value to be placed in the Avro record schema \"name\" field. The value must adhere to the Avro naming " + + "rules for fullname. If Expression Language is present then the evaluated value must adhere to the Avro naming rules.") .required(true) .expressionLanguageSupported(true) - .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .addValidator(StandardValidators.createRegexMatchingValidator(AVRO_RECORD_NAME_PATTERN)) .build(); public static final PropertyDescriptor CHARSET = new PropertyDescriptor.Builder() diff --git a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java index 171a64a892..e1a9ef9926 100644 --- a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java +++ b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java @@ -63,6 +63,30 @@ public class TestInferAvroSchema { runner.setProperty(InferAvroSchema.PRETTY_AVRO_OUTPUT, "true"); } + @Test + public void testRecordName() throws Exception { + + // Dot at the end is invalid + runner.setProperty(InferAvroSchema.RECORD_NAME, "org.apache.nifi.contact."); + runner.assertNotValid(); + // Dashes are invalid + runner.setProperty(InferAvroSchema.RECORD_NAME, "avro-schema"); + runner.assertNotValid(); + // Name cannot start with a digit + runner.setProperty(InferAvroSchema.RECORD_NAME, "1Record"); + runner.assertNotValid(); + // Name cannot start with a dot + runner.setProperty(InferAvroSchema.RECORD_NAME, ".record"); + runner.assertNotValid(); + + runner.setProperty(InferAvroSchema.RECORD_NAME, "avro_schema"); + runner.assertValid(); + runner.setProperty(InferAvroSchema.RECORD_NAME, "org.apache.nifi.contact"); + runner.assertValid(); + runner.setProperty(InferAvroSchema.RECORD_NAME, "${filename}"); // EL is valid, although its value may not be when evaluated + runner.assertValid(); + } + @Test public void inferAvroSchemaFromHeaderDefinitionOfCSVFile() throws Exception {