|
| 1 | +package com.fasterxml.jackson.dataformat.avro; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 5 | +import com.fasterxml.jackson.dataformat.avro.annotation.Decimal; |
| 6 | +import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator; |
| 7 | +import org.apache.avro.LogicalTypes; |
| 8 | +import org.apache.avro.Schema; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.math.BigDecimal; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +public class BigDecimal_schemaCreationTest extends AvroTestBase { |
| 16 | + private static final AvroMapper MAPPER = new AvroMapper(); |
| 17 | + |
| 18 | + static class BigDecimalWithDecimalAnnotationWrapper { |
| 19 | + @JsonProperty(required = true) // field is required to have simpler avro schema |
| 20 | + @Decimal(precision = 10, scale = 2) |
| 21 | + public final BigDecimal bigDecimalValue; |
| 22 | + |
| 23 | + public BigDecimalWithDecimalAnnotationWrapper(BigDecimal bigDecimalValue) { |
| 24 | + this.bigDecimalValue = bigDecimalValue; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testSchemaCreation_withLogicalTypesDisabled_onBigDecimalWithDecimalAnnotation() throws JsonMappingException { |
| 30 | + // GIVEN |
| 31 | + AvroSchemaGenerator gen = new AvroSchemaGenerator() |
| 32 | + .disableLogicalTypes(); |
| 33 | + |
| 34 | + // WHEN |
| 35 | + MAPPER.acceptJsonFormatVisitor(BigDecimalWithDecimalAnnotationWrapper.class, gen); |
| 36 | + // actualSchema = MAPPER.schemaFor(BigDecimalWithDecimalAnnotationWrapper.class) would be enough in this case |
| 37 | + // because logical types are disabled by default. |
| 38 | + final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
| 39 | + |
| 40 | + System.out.println(BigDecimalWithDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true)); |
| 41 | + |
| 42 | + // THEN |
| 43 | + assertThat(actualSchema.getField("bigDecimalValue")).isNotNull(); |
| 44 | + Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema(); |
| 45 | + assertThat(bigDecimalValue.getType()).isEqualTo(Schema.Type.STRING); |
| 46 | + assertThat(bigDecimalValue.getLogicalType()).isNull(); |
| 47 | + assertThat(bigDecimalValue.getProp("java-class")).isEqualTo("java.math.BigDecimal"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithDecimalAnnotation() throws JsonMappingException { |
| 52 | + // GIVEN |
| 53 | + AvroSchemaGenerator gen = new AvroSchemaGenerator() |
| 54 | + .enableLogicalTypes(); |
| 55 | + |
| 56 | + // WHEN |
| 57 | + MAPPER.acceptJsonFormatVisitor(BigDecimalWithDecimalAnnotationWrapper.class, gen); |
| 58 | + final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
| 59 | + |
| 60 | + System.out.println(BigDecimalWithDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true)); |
| 61 | + |
| 62 | + // THEN |
| 63 | + assertThat(actualSchema.getField("bigDecimalValue")).isNotNull(); |
| 64 | + Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema(); |
| 65 | + assertThat(bigDecimalValue.getType()).isEqualTo(Schema.Type.BYTES); |
| 66 | + assertThat(bigDecimalValue.getLogicalType()).isEqualTo(LogicalTypes.decimal(10, 2)); |
| 67 | + assertThat(bigDecimalValue.getProp("java-class")).isNull(); |
| 68 | + } |
| 69 | + |
| 70 | + static class BigDecimalWithDecimalAnnotationToFixedWrapper { |
| 71 | + @JsonProperty(required = true) // field is required to have simpler avro schema |
| 72 | + @AvroFixedSize(typeName = "BigDecimalWithDecimalAnnotationToFixedWrapper", size = 10) |
| 73 | + @Decimal(precision = 6, scale = 3) |
| 74 | + public final BigDecimal bigDecimalValue; |
| 75 | + |
| 76 | + public BigDecimalWithDecimalAnnotationToFixedWrapper(BigDecimal bigDecimalValue) { |
| 77 | + this.bigDecimalValue = bigDecimalValue; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithDecimalAnnotationToFixed() throws JsonMappingException { |
| 83 | + // GIVEN |
| 84 | + AvroSchemaGenerator gen = new AvroSchemaGenerator() |
| 85 | + .enableLogicalTypes(); |
| 86 | + |
| 87 | + // WHEN |
| 88 | + MAPPER.acceptJsonFormatVisitor(BigDecimalWithDecimalAnnotationToFixedWrapper.class, gen); |
| 89 | + final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
| 90 | + |
| 91 | + System.out.println(BigDecimalWithDecimalAnnotationToFixedWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true)); |
| 92 | + |
| 93 | + // THEN |
| 94 | + assertThat(actualSchema.getField("bigDecimalValue")).isNotNull(); |
| 95 | + |
| 96 | + Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema(); |
| 97 | + assertThat(bigDecimalValue.getType()).isEqualTo(Schema.Type.FIXED); |
| 98 | + assertThat(bigDecimalValue.getFixedSize()).isEqualTo(10); |
| 99 | + assertThat(bigDecimalValue.getLogicalType()).isEqualTo(LogicalTypes.decimal(6, 3)); |
| 100 | + assertThat(bigDecimalValue.getProp("java-class")).isNull(); |
| 101 | + } |
| 102 | +} |
0 commit comments