AVRO-2836 Generated java includes logical type conversions (#883)
* AVRO-2836 Generated java includes logical type conversions
If the logical type is used for a Fixed type (potentially also Enum)
then it should check if there are any conversions used, and
include in the generated java source
* AVRO-2836 Generated java includes logical type conversions
Move enum and fixed to use the javatype method, as if there is a
logical type conversion it will be picked up and remove a
code path
diff --git a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
index 317098f..9258a99 100644
--- a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
+++ b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
@@ -326,10 +326,10 @@
for (Schema s : schema.getTypes())
getClassNamesOfPrimitiveFields(s, result, seenSchemas);
break;
- case ENUM:
- case FIXED:
case NULL:
break;
+ case ENUM:
+ case FIXED:
case STRING:
case BYTES:
case INT:
@@ -337,7 +337,7 @@
case FLOAT:
case DOUBLE:
case BOOLEAN:
- result.add(javaType(schema));
+ result.add(javaType(schema, true));
break;
default:
throw new RuntimeException("Unknown type: " + schema);
diff --git a/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java b/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java
index f21572b..d452ccb 100644
--- a/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java
+++ b/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java
@@ -17,11 +17,20 @@
*/
package org.apache.avro.codegentest;
-import java.time.LocalDate;
-
-import org.apache.avro.codegentest.testdata.*;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesArray;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesMap;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesRecord;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesUnion;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesUnionFixedDecimal;
+import org.apache.avro.codegentest.testdata.NestedRecord;
+import org.apache.avro.codegentest.testdata.NullableLogicalTypesArray;
+import org.apache.avro.codegentest.testdata.RecordInArray;
+import org.apache.avro.codegentest.testdata.RecordInMap;
+import org.apache.avro.codegentest.testdata.RecordInUnion;
import org.junit.Test;
+import java.math.BigInteger;
+import java.time.LocalDate;
import java.util.Collections;
public class TestNestedLogicalTypes extends AbstractSpecificRecordTest {
@@ -64,4 +73,12 @@
.build();
verifySerDeAndStandardMethods(nestedLogicalTypesMap);
}
+
+ @Test
+ public void testNullableLogicalTypeInRecordInFixedDecimal() {
+ final NestedLogicalTypesUnionFixedDecimal nestedLogicalTypesUnionFixedDecimal = NestedLogicalTypesUnionFixedDecimal
+ .newBuilder().setUnionOfFixedDecimal(new CustomDecimal(BigInteger.TEN, 15)).build();
+ verifySerDeAndStandardMethods(nestedLogicalTypesUnionFixedDecimal);
+ }
+
}
diff --git a/lang/java/integration-test/codegen-test/src/test/resources/avro/nested_logical_types_union_fixed.avsc b/lang/java/integration-test/codegen-test/src/test/resources/avro/nested_logical_types_union_fixed.avsc
new file mode 100644
index 0000000..d2dbb4b
--- /dev/null
+++ b/lang/java/integration-test/codegen-test/src/test/resources/avro/nested_logical_types_union_fixed.avsc
@@ -0,0 +1,21 @@
+{"namespace": "org.apache.avro.codegentest.testdata",
+ "type": "record",
+ "name": "NestedLogicalTypesUnionFixedDecimal",
+ "doc" : "Test nested types with logical types in generated Java classes",
+ "fields": [
+ {
+ "name": "unionOfFixedDecimal",
+ "type": ["null", {
+ "namespace": "org.apache.avro.codegentest.testdata",
+ "name": "FixedInUnion",
+ "type": "fixed",
+ "size": 12,
+ "logicalType": "decimal",
+ "precision": 28,
+ "scale": 15
+ }]
+ }]
+}
+
+
+
diff --git a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java
index 991d58a..5b7514c 100644
--- a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java
+++ b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java
@@ -45,6 +45,10 @@
}
+ int signum() {
+ return internalValue.signum();
+ }
+
@Override
public boolean equals(Object o) {
if (this == o)
diff --git a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java
index 845772b..1857ea2 100644
--- a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java
+++ b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java
@@ -22,9 +22,12 @@
import org.apache.avro.LogicalType;
import org.apache.avro.LogicalTypes;
import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericFixed;
import java.math.BigInteger;
import java.nio.ByteBuffer;
+import java.util.Arrays;
public class CustomDecimalConversion extends Conversion<CustomDecimal> {
@Override
@@ -49,4 +52,25 @@
int scale = ((LogicalTypes.Decimal) type).getScale();
return ByteBuffer.wrap(value.toByteArray(scale));
}
+
+ @Override
+ public CustomDecimal fromFixed(GenericFixed value, Schema schema, LogicalType type) {
+ int scale = ((LogicalTypes.Decimal) type).getScale();
+ return new CustomDecimal(new BigInteger(value.bytes()), scale);
+ }
+
+ @Override
+ public GenericFixed toFixed(CustomDecimal value, Schema schema, LogicalType type) {
+ int scale = ((LogicalTypes.Decimal) type).getScale();
+ byte fillByte = (byte) (value.signum() < 0 ? 0xFF : 0x00);
+ byte[] unscaled = value.toByteArray(scale);
+ byte[] bytes = new byte[schema.getFixedSize()];
+ int offset = bytes.length - unscaled.length;
+
+ // Fill the front of the array and copy remaining with unscaled values
+ Arrays.fill(bytes, 0, offset, fillByte);
+ System.arraycopy(unscaled, 0, bytes, offset, bytes.length - offset);
+
+ return new GenericData.Fixed(schema, bytes);
+ }
}