avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1646363 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
Date Wed, 17 Dec 2014 23:20:41 GMT
Author: cutting
Date: Wed Dec 17 23:20:41 2014
New Revision: 1646363

URL: http://svn.apache.org/r1646363
Log:
AVRO-1623. Java: Fix GenericData#validate to correctly resolve unions.  Contributed by Jeffrey
Mullins.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1646363&r1=1646362&r2=1646363&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Dec 17 23:20:41 2014
@@ -99,6 +99,9 @@ Trunk (not yet released)
     AVRO-1545. Python. Fix to retain schema properties on primitive types.
     (Dustin Spicuzza via cutting)
 
+    AVRO-1623. Java: Fix GenericData#validate to correctly resolve unions.
+    (Jeffrey Mullins via cutting)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java?rev=1646363&r1=1646362&r2=1646363&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java Wed Dec
17 23:20:41 2014
@@ -400,10 +400,12 @@ public class GenericData {
           return false;
       return true;
     case UNION:
-      for (Schema type : schema.getTypes())
-        if (validate(type, datum))
-          return true;
-      return false;
+      try {
+        int i = resolveUnion(schema, datum);
+        return validate(schema.getTypes().get(i), datum);
+      } catch (UnresolvedUnionException e) {
+        return false;
+      }
     case FIXED:
       return datum instanceof GenericFixed
         && ((GenericFixed)datum).bytes().length==schema.getFixedSize();

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java?rev=1646363&r1=1646362&r2=1646363&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java Wed
Dec 17 23:20:41 2014
@@ -33,6 +33,7 @@ import org.apache.avro.Schema;
 import org.apache.avro.Schema.Field;
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema.Type;
+import org.apache.avro.SchemaBuilder;
 import org.apache.avro.io.BinaryData;
 import org.apache.avro.io.BinaryEncoder;
 import org.apache.avro.io.EncoderFactory;
@@ -455,4 +456,26 @@ public class TestGenericData {
     assertFalse("We don't expect GenericData to allow a String datum for an enum schema",
gd.validate(schema, "ONE"));
     assertFalse("We don't expect GenericData to allow a Java Enum for an enum schema", gd.validate(schema,
anEnum.ONE));
   }
+
+  @Test
+  public void testValidateUnion() {
+      Schema type1Schema = SchemaBuilder.record("Type1")
+          .fields()
+          .requiredString("myString")
+          .requiredInt("myInt")
+          .endRecord();
+
+      Schema type2Schema = SchemaBuilder.record("Type2")
+          .fields()
+          .requiredString("myString")
+          .endRecord();
+
+      Schema unionSchema = SchemaBuilder.unionOf()
+          .type(type1Schema).and().type(type2Schema)
+          .endUnion();
+
+    GenericRecord record = new GenericData.Record(type2Schema);
+    record.put("myString", "myValue");
+    assertTrue(GenericData.get().validate(unionSchema, record));
+  }
 }



Mime
View raw message