avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1520141 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
Date Wed, 04 Sep 2013 20:59:57 GMT
Author: cutting
Date: Wed Sep  4 20:59:57 2013
New Revision: 1520141

URL: http://svn.apache.org/r1520141
Log:
AVRO-1368. Fix SpecificDatumWriter to, when writing a string schema, not silently convert
any object to a string.  Contributed by Christophe Taton.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1520141&r1=1520140&r2=1520141&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Sep  4 20:59:57 2013
@@ -13,6 +13,10 @@ Trunk (not yet released)
 
   BUG FIXES
 
+    AVRO-1368. Fix SpecificDatumWriter to, when writing a string
+    schema, not silently convert any object to a string.
+    (Christophe Taton via cutting)
+
 Avro 1.7.5 (12 August 2013)
 
   NEW FEATURES

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java?rev=1520141&r1=1520140&r2=1520141&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java
Wed Sep  4 20:59:57 2013
@@ -45,6 +45,11 @@ public class SpecificDatumWriter<T> exte
     super(specificData);
   }
   
+  /** Returns the {@link SpecificData} implementation used by this writer. */
+  public SpecificData getSpecificData() {
+    return (SpecificData) getData();
+  }
+
   @Override
   protected void writeEnum(Schema schema, Object datum, Encoder out)
     throws IOException {
@@ -57,8 +62,10 @@ public class SpecificDatumWriter<T> exte
   @Override
   protected void writeString(Schema schema, Object datum, Encoder out)
     throws IOException {
-    if (!(datum instanceof CharSequence))         // Stringable
-      datum = datum.toString();                   // call toString()
+    if (!(datum instanceof CharSequence)
+        && getSpecificData().isStringable(datum.getClass())) {
+      datum = datum.toString();                   // convert to string
+    }
     writeString(datum, out);
   }
 

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java?rev=1520141&r1=1520140&r2=1520141&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
(original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
Wed Sep  4 20:59:57 2013
@@ -22,7 +22,9 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import java.io.ByteArrayOutputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -30,6 +32,9 @@ import java.util.List;
 import org.apache.avro.Schema;
 import org.apache.avro.Schema.Field;
 import org.apache.avro.Schema.Type;
+import org.apache.avro.io.DatumWriter;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -125,4 +130,19 @@ public class TestSpecificData {
     assertEquals(1, record.get("x"));
     assertEquals("str", record.get("y"));
   }
+
+  /** Tests that non Stringable datum are rejected by specific writers. */
+  @Test
+  public void testNonStringable() throws Exception {
+    final Schema string = Schema.create(Type.STRING);
+    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    final Encoder encoder = EncoderFactory.get().directBinaryEncoder(baos, null);
+    final DatumWriter<Object> writer = new SpecificDatumWriter<Object>(string);
+    try {
+      writer.write(new Object(), encoder);
+      fail("Non stringable object should be rejected.");
+    } catch (ClassCastException cce) {
+      // Expected error
+    }
+  }
 }



Mime
View raw message