avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rskr...@apache.org
Subject [avro] branch master updated: AVRO-3144 Support for enabling and disabling default value validation through SchemaBuilder (#1276)
Date Fri, 02 Jul 2021 20:29:51 GMT
This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 8dd55db  AVRO-3144 Support for enabling and disabling default value validation through
SchemaBuilder (#1276)
8dd55db is described below

commit 8dd55dbac12103c4e81be199a7d819e8e8fb5b56
Author: Andreas Hailu <6646502+hailuand@users.noreply.github.com>
AuthorDate: Fri Jul 2 16:29:40 2021 -0400

    AVRO-3144 Support for enabling and disabling default value validation through SchemaBuilder
(#1276)
    
    * AVRO-3144 Support for enabling and disabling default value validation through SchemaBuilder
    
    * AVRO-3144 Fix spotless violations
---
 .../main/java/org/apache/avro/SchemaBuilder.java   | 20 ++++++++++++++++++-
 .../java/org/apache/avro/TestSchemaBuilder.java    | 23 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/SchemaBuilder.java b/lang/java/avro/src/main/java/org/apache/avro/SchemaBuilder.java
index ffb198f..818f89a 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/SchemaBuilder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/SchemaBuilder.java
@@ -2150,6 +2150,7 @@ public class SchemaBuilder {
   public final static class FieldBuilder<R> extends NamedBuilder<FieldBuilder<R>>
{
     private final FieldAssembler<R> fields;
     private Schema.Field.Order order = Schema.Field.Order.ASCENDING;
+    private boolean validatingDefaults = true;
 
     private FieldBuilder(FieldAssembler<R> fields, NameContext names, String name)
{
       super(names, name);
@@ -2175,6 +2176,23 @@ public class SchemaBuilder {
     }
 
     /**
+     * Validate field default value during {@link #completeField(Schema, JsonNode)}.
+     **/
+    public FieldBuilder<R> validatingDefaults() {
+      validatingDefaults = true;
+      return self();
+    }
+
+    /**
+     * Skip field default value validation during
+     * {@link #completeField(Schema, JsonNode)}}
+     **/
+    public FieldBuilder<R> notValidatingDefaults() {
+      validatingDefaults = false;
+      return self();
+    }
+
+    /**
      * Final step in configuring this field, finalizing name, namespace, alias, and
      * order.
      *
@@ -2237,7 +2255,7 @@ public class SchemaBuilder {
     }
 
     private FieldAssembler<R> completeField(Schema schema, JsonNode defaultVal) {
-      Field field = new Field(name(), schema, doc(), defaultVal, true, order);
+      Field field = new Field(name(), schema, doc(), defaultVal, validatingDefaults, order);
       addPropsTo(field);
       addAliasesTo(field);
       return fields.addField(field);
diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java b/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java
index a5f5c13..77ee588 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java
@@ -818,4 +818,27 @@ public class TestSchemaBuilder {
         schema.getField("double").defaultVal());
   }
 
+  @Test(expected = AvroRuntimeException.class)
+  public void testValidateDefaultsEnabled() {
+    try {
+      SchemaBuilder.record("ValidationRecord").fields().name("IntegerField").type("int").withDefault("Invalid")
+          .endRecord();
+    } catch (AvroRuntimeException e) {
+      Assert.assertEquals("Default behavior is to raise an exception due to record having
an invalid default",
+          "Invalid default for field IntegerField: \"Invalid\" not a \"int\"", e.getMessage());
+      throw e;
+    }
+  }
+
+  @Test
+  public void testValidateDefaultsDisabled() {
+    final String fieldName = "IntegerField";
+    final String defaultValue = "foo";
+    Schema schema = SchemaBuilder.record("ValidationRecord").fields().name(fieldName).notValidatingDefaults()
+        .type("int").withDefault(defaultValue) // Would throw an exception on endRecord()
if validations enabled
+        .endRecord();
+    Assert.assertNull("Differing types, so this returns null", schema.getField(fieldName).defaultVal());
+    Assert.assertEquals("Schema is able to be successfully created as is without validation",
defaultValue,
+        schema.getField(fieldName).defaultValue().asText());
+  }
 }

Mime
View raw message