Skip to content

Commit

Permalink
Updating validate to use AvroCompatHelper to get field default value (#…
Browse files Browse the repository at this point in the history
…512)

* Updating validate to use AvroCompatHelper to get field default value

* Adding UT to check validate in CompatibleSpecificRecordBuilderBase

* Checkstyle update for new file
  • Loading branch information
li-ukumar authored Sep 7, 2023
1 parent 4640b55 commit c16ffa1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down
3 changes: 3 additions & 0 deletions helper/tests/helper-tests-110/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit c16ffa1

Please sign in to comment.