[ 
https://issues.apache.org/jira/browse/AVRO-1954?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16390034#comment-16390034
 ] 

ASF GitHub Bot commented on AVRO-1954:
--------------------------------------

derekstraka closed pull request #290: AVRO-1954 - Schema.Field.defaultVal() 
generates: Unknown datum type org.apache.avro.JsonProperties$Null
URL: https://github.com/apache/avro/pull/290
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/CHANGES.txt b/CHANGES.txt
index 7156f730e..83f214592 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -40,6 +40,9 @@ Trunk (not yet released)
     AVRO-2122: Cannot validate schemas with recursive definitions
     (Bart via Nandor Kollar)
 
+    AVRO-1954: Java: Schema.Field.defaultVal() generates: Unknown datum type
+    (Nandor Kollar via tomwhite)
+
 Avro 1.8.2 (10 April 2017)
 
   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 67d47d99c..c7931c736 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.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 int resolveUnion(Schema union, Object datum) {
   /** 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 f93a326c4..8f99cdfe4 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 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.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 void writeDoesNotAllowJavaEnumForGenericEnum() 
throws IOException {
 
     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;
+  }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Schema.Field.defaultVal() generates: Unknown datum type 
> org.apache.avro.JsonProperties$Null
> -------------------------------------------------------------------------------------------
>
>                 Key: AVRO-1954
>                 URL: https://issues.apache.org/jira/browse/AVRO-1954
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.8.1, 1.9.0
>            Reporter: rui miranda
>            Assignee: Nandor Kollar
>            Priority: Minor
>             Fix For: 1.9.0
>
>         Attachments: unitTestDefaultNull.patch
>
>
> I was creating GenericRecords and populating some fields -- which i could not 
> find the content on some json files -- with the Schema.Field.defaultVal(). 
> It seems if the schema has explicitly set the default value to be null, the 
> records generated this way can't be written. In this case, if default value 
> is null in the schema, an instance of 
> org.apache.avro.JsonProperties.NULL_VALUE is returned by 
> Schema.Field.defaultVal().
> I created an unit test which replicates the bug. I was thinking modify the 
> class org.apache.avro.generic.GenericData to evaluate 
> org.apache.avro.JsonProperties.NULL_VALUE as null. Is this the way to go? or 
> org.apache.avro.JsonProperties.NULL_VALUE is intend for other purposes?



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to