You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2014/09/23 00:13:38 UTC

svn commit: r1626913 - 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/reflect/TestReflectAllowNulls.java

Author: cutting
Date: Mon Sep 22 22:13:38 2014
New Revision: 1626913

URL: http://svn.apache.org/r1626913
Log:
AVRO-1589. Java: Fix ReflectData.AllowNulls to not create unions for primitive types.  Contributed by Ryan Blue.

Added:
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectAllowNulls.java   (with props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1626913&r1=1626912&r2=1626913&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Mon Sep 22 22:13:38 2014
@@ -30,6 +30,9 @@ Trunk (not yet released)
     AVRO-1544. Java: Fix GenericData#validate for unions with null.
     (Matthew Hayes via cutting)
 
+    AVRO-1589. Java: Fix ReflectData.AllowNulls to not create unions
+    for primitive types. (Ryan Blue via cutting)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

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=1626913&r1=1626912&r2=1626913&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 Sep 22 22:13:38 2014
@@ -73,6 +73,11 @@ public class ReflectData extends Specifi
     @Override
     protected Schema createFieldSchema(Field field, Map<String, Schema> names) {
       Schema schema = super.createFieldSchema(field, names);
+      if (field.getType().isPrimitive()) {
+        // for primitive values, such as int, a null will result in a
+        // NullPointerException at read time
+        return schema;
+      }
       return makeNullable(schema);
     }
   }

Added: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectAllowNulls.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectAllowNulls.java?rev=1626913&view=auto
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectAllowNulls.java (added)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectAllowNulls.java Mon Sep 22 22:13:38 2014
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.reflect;
+
+import java.util.Arrays;
+import org.apache.avro.Schema;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestReflectAllowNulls {
+
+  private static class Primitives {
+    boolean aBoolean;
+    byte aByte;
+    short aShort;
+    int anInt;
+    long aLong;
+    float aFloat;
+    double aDouble;
+  }
+
+  private static class Wrappers {
+    Boolean aBoolean;
+    Byte aByte;
+    Short aShort;
+    Integer anInt;
+    Long aLong;
+    Float aFloat;
+    Double aDouble;
+    Primitives anObject;
+  }
+
+  @Test
+  public void testPrimitives() {
+    // AllowNull only makes fields nullable, so testing must use a base record
+    Schema primitives = ReflectData.AllowNull.get().getSchema(Primitives.class);
+    Assert.assertEquals(requiredSchema(boolean.class),
+        primitives.getField("aBoolean").schema());
+    Assert.assertEquals(requiredSchema(byte.class),
+        primitives.getField("aByte").schema());
+    Assert.assertEquals(requiredSchema(short.class),
+        primitives.getField("aShort").schema());
+    Assert.assertEquals(requiredSchema(int.class),
+        primitives.getField("anInt").schema());
+    Assert.assertEquals(requiredSchema(long.class),
+        primitives.getField("aLong").schema());
+    Assert.assertEquals(requiredSchema(float.class),
+        primitives.getField("aFloat").schema());
+    Assert.assertEquals(requiredSchema(double.class),
+        primitives.getField("aDouble").schema());
+  }
+
+  @Test
+  public void testWrappers() {
+    // AllowNull only makes fields nullable, so testing must use a base record
+    Schema wrappers = ReflectData.AllowNull.get().getSchema(Wrappers.class);
+    Assert.assertEquals(nullableSchema(boolean.class),
+        wrappers.getField("aBoolean").schema());
+    Assert.assertEquals(nullableSchema(byte.class),
+        wrappers.getField("aByte").schema());
+    Assert.assertEquals(nullableSchema(short.class),
+        wrappers.getField("aShort").schema());
+    Assert.assertEquals(nullableSchema(int.class),
+        wrappers.getField("anInt").schema());
+    Assert.assertEquals(nullableSchema(long.class),
+        wrappers.getField("aLong").schema());
+    Assert.assertEquals(nullableSchema(float.class),
+        wrappers.getField("aFloat").schema());
+    Assert.assertEquals(nullableSchema(double.class),
+        wrappers.getField("aDouble").schema());
+    Assert.assertEquals(nullableSchema(Primitives.class),
+        wrappers.getField("anObject").schema());
+  }
+
+  private Schema requiredSchema(Class<?> type) {
+    return ReflectData.get().getSchema(type);
+  }
+
+  private Schema nullableSchema(Class<?> type) {
+    return Schema.createUnion(Arrays.asList(
+        Schema.create(Schema.Type.NULL),
+        ReflectData.get().getSchema(type)));
+  }
+}

Propchange: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectAllowNulls.java
------------------------------------------------------------------------------
    svn:eol-style = native