avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tomwh...@apache.org
Subject avro git commit: AVRO-1954 - Schema.Field.defaultVal() generates: Unknown datum type org.apache.avro.JsonProperties$Null. Contributed by Nandor Kollar
Date Fri, 20 Jan 2017 11:52:47 GMT
Repository: avro
Updated Branches:
  refs/heads/master 4f53fa933 -> d9338a4cf


AVRO-1954 - Schema.Field.defaultVal() generates: Unknown datum type org.apache.avro.JsonProperties$Null.
Contributed by Nandor Kollar


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/d9338a4c
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/d9338a4c
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/d9338a4c

Branch: refs/heads/master
Commit: d9338a4cf008b445ea3efbe2523288d07162ec71
Parents: 4f53fa9
Author: Nandor Kollar <nkollar@cloudera.com>
Authored: Mon Jan 2 12:00:34 2017 +0100
Committer: Tom White <tom@cloudera.com>
Committed: Fri Jan 20 11:52:11 2017 +0000

----------------------------------------------------------------------
 CHANGES.txt                                     |  3 ++
 .../org/apache/avro/generic/GenericData.java    |  3 +-
 .../avro/generic/TestGenericDatumWriter.java    | 43 ++++++++++++++++++--
 3 files changed, 45 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/d9338a4c/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 2634eef..fbb1c2f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -114,6 +114,9 @@ Trunk (not yet released)
     AVRO-1881: Java: Avro (Java) Memory Leak when reusing JsonDecoder instance.
     (Nandor Kollar via tomwhite)
 
+    AVRO-1954: Java: Schema.Field.defaultVal() generates: Unknown datum type
+    (Nandor Kollar via tomwhite)
+
 Avro 1.8.1 (14 May 2016)
 
   INCOMPATIBLE CHANGES

http://git-wip-us.apache.org/repos/asf/avro/blob/d9338a4c/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
----------------------------------------------------------------------
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 ff0893c..96f5ad5 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
@@ -35,6 +35,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;
@@ -743,7 +744,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();

http://git-wip-us.apache.org/repos/asf/avro/blob/d9338a4c/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericDatumWriter.java
----------------------------------------------------------------------
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;
+  }
 }


Mime
View raw message