This is an automated email from the ASF dual-hosted git repository. nkollar pushed a commit to branch branch-1.8 in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/branch-1.8 by this push: new 10792f7 AVRO-1954 - Schema.Field.defaultVal() generates: Unknown datum type org.apache.avro.JsonProperties$Null. Contributed by Nandor Kollar 10792f7 is described below commit 10792f777f5a31f8c1e5d9a6f6c6d4ba2727ab58 Author: Nandor Kollar <nkol...@cloudera.com> AuthorDate: Mon Jan 2 12:00:34 2017 +0100 AVRO-1954 - Schema.Field.defaultVal() generates: Unknown datum type org.apache.avro.JsonProperties$Null. Contributed by Nandor Kollar (cherry picked from commit d9338a4cf008b445ea3efbe2523288d07162ec71) --- CHANGES.txt | 5 +++ .../java/org/apache/avro/generic/GenericData.java | 3 +- .../avro/generic/TestGenericDatumWriter.java | 43 ++++++++++++++++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 7156f73..e712af0 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -40,6 +40,8 @@ Trunk (not yet released) AVRO-2122: Cannot validate schemas with recursive definitions (Bart via Nandor Kollar) + AVRO-1954 - Schema.Field.defaultVal() generates: Unknown datum type org.apache.avro.JsonProperties$Null. + Avro 1.8.2 (10 April 2017) INCOMPATIBLE CHANGES @@ -147,6 +149,9 @@ Avro 1.8.2 (10 April 2017) AVRO-1813: Incorrect link to build instructions in Java Getting Started (Pietro Menna via gabor) + AVRO-1954: Java: Schema.Field.defaultVal() generates: Unknown datum type + (Nandor Kollar via tomwhite) + Avro 1.8.1 (14 May 2016) INCOMPATIBLE CHANGES diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java index 67d47d9..c7931c7 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java @@ -36,6 +36,7 @@ import org.apache.avro.AvroRuntimeException; import org.apache.avro.AvroTypeException; import org.apache.avro.Conversion; import org.apache.avro.Conversions; +import org.apache.avro.JsonProperties; import org.apache.avro.LogicalType; import org.apache.avro.Schema; import org.apache.avro.Schema.Field; @@ -744,7 +745,7 @@ public class GenericData { /** Return the schema full name for a datum. Called by {@link * #resolveUnion(Schema,Object)}. */ protected String getSchemaName(Object datum) { - if (datum == null) + if (datum == null || datum == JsonProperties.NULL_VALUE) return Type.NULL.getName(); if (isRecord(datum)) return getRecordSchema(datum).getFullName(); diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericDatumWriter.java b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericDatumWriter.java index f93a326..8f99cdf 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericDatumWriter.java +++ b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericDatumWriter.java @@ -24,7 +24,6 @@ import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.nio.ByteBuffer; import java.util.ConcurrentModificationException; import java.util.HashMap; @@ -37,11 +36,10 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.apache.avro.Schema; +import org.apache.avro.io.BinaryEncoder; import org.apache.avro.io.DecoderFactory; -import org.apache.avro.io.DirectBinaryEncoder; import org.apache.avro.io.Encoder; import org.apache.avro.io.EncoderFactory; -import org.apache.avro.io.JsonDecoder; import org.apache.avro.AvroTypeException; import org.junit.Test; import org.apache.avro.util.Utf8; @@ -257,4 +255,43 @@ public class TestGenericDatumWriter { writer.write(record, encoder); } + + @Test + public void writeFieldWithDefaultWithExplicitNullDefaultInSchema() throws Exception { + Schema schema = schemaWithExplicitNullDefault(); + GenericRecord record = createRecordWithDefaultField(schema); + writeObject(schema, record); + } + + @Test + public void writeFieldWithDefaultWithoutExplicitNullDefaultInSchema() throws Exception { + Schema schema = schemaWithoutExplicitNullDefault(); + GenericRecord record = createRecordWithDefaultField(schema); + writeObject(schema, record); + } + + private Schema schemaWithExplicitNullDefault() { + String schema = "{\"type\":\"record\",\"name\":\"my_record\",\"namespace\":\"mytest.namespace\",\"doc\":\"doc\"," + + "\"fields\":[{\"name\":\"f\",\"type\":[\"null\",\"string\"],\"doc\":\"field doc doc\", " + + "\"default\":null}]}"; + return new Schema.Parser().parse(schema); + } + + private Schema schemaWithoutExplicitNullDefault() { + String schema = "{\"type\":\"record\",\"name\":\"my_record\",\"namespace\":\"mytest.namespace\",\"doc\":\"doc\"," + + "\"fields\":[{\"name\":\"f\",\"type\":[\"null\",\"string\"],\"doc\":\"field doc doc\"}]}"; + return new Schema.Parser().parse(schema); + } + + private void writeObject(Schema schema, GenericRecord datum) throws Exception { + BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(new ByteArrayOutputStream(), null); + GenericDatumWriter<GenericData.Record> writer = new GenericDatumWriter<GenericData.Record>(schema); + writer.write(schema, datum, encoder); + } + + private GenericRecord createRecordWithDefaultField(Schema schema) { + GenericRecord record = new GenericData.Record(schema); + record.put("f", schema.getField("f").defaultVal()); + return record; + } } -- To stop receiving notification emails like this one, please contact nkol...@apache.org.