avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1188382 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java lang/java/avro/src/test/java/org/apache/avro/TestReflect.java
Date Mon, 24 Oct 2011 21:09:02 GMT
Author: cutting
Date: Mon Oct 24 21:09:01 2011
New Revision: 1188382

URL: http://svn.apache.org/viewvc?rev=1188382&view=rev
Log:
AVRO-942. Java: Fix reflect so that @Nullable fields have a default value of null.  Contributed
by Binglin Chang.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1188382&r1=1188381&r2=1188382&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Mon Oct 24 21:09:01 2011
@@ -220,6 +220,9 @@ Avro 1.6.0 (unreleased)
     AVRO-930. C: Fixed memory lead in resolved writer class. (Vivek
     Nadkarni via dcreager)
 
+    AVRO-942. Java: Fix reflect so that @Nullable fields have a
+    default value of null. (Binglin Chang via cutting)
+
 Avro 1.5.4 (12 September 2011)
 
   IMPROVEMENTS

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java?rev=1188382&r1=1188381&r2=1188382&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java Mon Oct
24 21:09:01 2011
@@ -45,6 +45,8 @@ import org.apache.avro.generic.GenericCo
 import org.apache.avro.specific.SpecificData;
 import org.apache.avro.specific.FixedSize;
 import org.apache.avro.io.BinaryData;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.node.NullNode;
 
 import com.thoughtworks.paranamer.CachingParanamer;
 import com.thoughtworks.paranamer.Paranamer;
@@ -288,8 +290,15 @@ public class ReflectData extends Specifi
           for (Field field : getFields(c))
             if ((field.getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0){
               Schema fieldSchema = createFieldSchema(field, names);
+              JsonNode defaultValue = null;
+              if (fieldSchema.getType() == Schema.Type.UNION) {
+                Schema defaultType = fieldSchema.getTypes().get(0);
+                if (defaultType.getType() == Schema.Type.NULL) {
+                  defaultValue = NullNode.getInstance();
+                }
+              }
               fields.add(new Schema.Field(field.getName(),
-                  fieldSchema, null /* doc */, null));
+                  fieldSchema, null /* doc */, defaultValue));
             }
           if (error)                              // add Throwable message
             fields.add(new Schema.Field("detailMessage", THROWABLE_MESSAGE,

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java?rev=1188382&r1=1188381&r2=1188382&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java Mon Oct 24 21:09:01
2011
@@ -32,6 +32,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.codehaus.jackson.node.NullNode;
+
 import org.apache.avro.Schema.Field;
 import org.apache.avro.TestReflect.SampleRecord.AnotherSampleRecord;
 import org.apache.avro.io.DecoderFactory;
@@ -289,10 +291,12 @@ public class TestReflect {
   @Test public void testR11() throws Exception {
     Schema r11Record = ReflectData.get().getSchema(R11.class);
     assertEquals(Schema.Type.RECORD, r11Record.getType());
-    Schema r11Field = r11Record.getField("text").schema();
-    assertEquals(Schema.Type.UNION, r11Field.getType());
-    assertEquals(Schema.Type.NULL, r11Field.getTypes().get(0).getType());
-    Schema r11String = r11Field.getTypes().get(1);
+    Field r11Field = r11Record.getField("text");
+    assertEquals(NullNode.getInstance(), r11Field.defaultValue());
+    Schema r11FieldSchema = r11Field.schema();
+    assertEquals(Schema.Type.UNION, r11FieldSchema.getType());
+    assertEquals(Schema.Type.NULL, r11FieldSchema.getTypes().get(0).getType());
+    Schema r11String = r11FieldSchema.getTypes().get(1);
     assertEquals(Schema.Type.STRING, r11String.getType());
     R11 r11 = new R11();
     checkReadWrite(r11, r11Record);



Mime
View raw message