From c16ffa1ee07ec3b3b4acd5c6bda12ae6da0e303e Mon Sep 17 00:00:00 2001 From: Uttam Kumar Date: Thu, 7 Sep 2023 05:37:49 +0000 Subject: [PATCH] Updating validate to use AvroCompatHelper to get field default value (#512) * Updating validate to use AvroCompatHelper to get field default value * Adding UT to check validate in CompatibleSpecificRecordBuilderBase * Checkstyle update for new file --- .../CompatibleSpecificRecordBuilderBase.java | 2 +- helper/tests/helper-tests-110/build.gradle | 3 ++ ...mpatibleSpecificRecordBuilderBaseTest.java | 34 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 helper/tests/helper-tests-110/src/test/java/com/linkedin/avroutil1/compatibility/avro110/CompatibleSpecificRecordBuilderBaseTest.java diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/CompatibleSpecificRecordBuilderBase.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/CompatibleSpecificRecordBuilderBase.java index 1494e0f65..1661cb4b7 100644 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/CompatibleSpecificRecordBuilderBase.java +++ b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/CompatibleSpecificRecordBuilderBase.java @@ -48,7 +48,7 @@ protected final boolean[] fieldSetFlags() { * accept null values. */ protected void validate(Schema.Field field, Object value) { - if (!isValidValue(field, value) && field.defaultValue() == null) { + if (!isValidValue(field, value) && AvroCompatibilityHelper.getSpecificDefaultValue(field) == null) { throw new AvroRuntimeException("Field " + field + " does not accept null values"); } } diff --git a/helper/tests/helper-tests-110/build.gradle b/helper/tests/helper-tests-110/build.gradle index 7193c6a31..486fd1f15 100644 --- a/helper/tests/helper-tests-110/build.gradle +++ b/helper/tests/helper-tests-110/build.gradle @@ -43,4 +43,7 @@ dependencies { testImplementation "org.apache.avro:avro-compiler:1.10.2" testImplementation "com.google.guava:guava:28.2-jre" testImplementation "org.mockito:mockito-core:3.2.4" + + testImplementation group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.8.10' + } \ No newline at end of file diff --git a/helper/tests/helper-tests-110/src/test/java/com/linkedin/avroutil1/compatibility/avro110/CompatibleSpecificRecordBuilderBaseTest.java b/helper/tests/helper-tests-110/src/test/java/com/linkedin/avroutil1/compatibility/avro110/CompatibleSpecificRecordBuilderBaseTest.java new file mode 100644 index 000000000..efa5c0794 --- /dev/null +++ b/helper/tests/helper-tests-110/src/test/java/com/linkedin/avroutil1/compatibility/avro110/CompatibleSpecificRecordBuilderBaseTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2023 LinkedIn Corp. + * Licensed under the BSD 2-Clause License (the "License"). + * See License in the project root for license information. + */ + +package com.linkedin.avroutil1.compatibility.avro110; + +import com.linkedin.avroutil1.compatibility.CompatibleSpecificRecordBuilderBase; +import java.util.Arrays; +import org.apache.avro.Schema; +import org.testng.annotations.Test; + + +public class CompatibleSpecificRecordBuilderBaseTest { + + @Test + public void testValidate() { + Schema.Field field = new Schema.Field("field1", Schema.create(Schema.Type.STRING), "field doc", "null"); + Schema myRecord = Schema.createRecord(Arrays.asList(field)); + new MyBuilderClass(myRecord).testValidate(field, null); + } + + class MyBuilderClass extends CompatibleSpecificRecordBuilderBase { + + protected MyBuilderClass(Schema schema) { + super(schema); + } + + public void testValidate(Schema.Field f, Object o) { + validate(f, o); + } + } +} \ No newline at end of file