avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1539496 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator.java
Date Wed, 06 Nov 2013 23:47:22 GMT
Author: cutting
Date: Wed Nov  6 23:47:22 2013
New Revision: 1539496

URL: http://svn.apache.org/r1539496
Log:
AVRO-1397. Java: Improve error message when missing field has no default.  Contributed by
David Carr.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1539496&r1=1539495&r2=1539496&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Nov  6 23:47:22 2013
@@ -20,6 +20,9 @@ Trunk (not yet released)
     AVRO-1384. Java: Permit Maven to find imports within project.
     (Alexandre Normand via cutting)
 
+    AVRO-1397. Java: Improve error message when missing field has no default.
+    (David Carr via cutting)
+
   BUG FIXES
 
     AVRO-1368. Fix SpecificDatumWriter to, when writing a string

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java?rev=1539496&r1=1539495&r2=1539496&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
Wed Nov  6 23:47:22 2013
@@ -223,7 +223,8 @@ public class ResolvingGrammarGenerator e
         if (writer.getField(fname) == null) {
           if (rf.defaultValue() == null) {
             result = Symbol.error("Found " + writer.getFullName()
-                                  + ", expecting " + reader.getFullName());
+                                  + ", expecting " + reader.getFullName()
+                                  + ", missing required field " + fname);
             seen.put(wsc, result);
             return result;
           } else {

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator.java?rev=1539496&r1=1539495&r2=1539496&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator.java
(original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator.java
Wed Nov  6 23:47:22 2013
@@ -17,18 +17,28 @@
  */
 package org.apache.avro.io.parsing;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.StringReader;
 import java.util.Arrays;
 import java.util.Collection;
 
+import org.apache.avro.AvroTypeException;
 import org.apache.avro.Schema;
+import org.apache.avro.SchemaBuilder;
+import org.apache.avro.file.DataFileStream;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecordBuilder;
 import org.apache.avro.io.Encoder;
 import org.apache.avro.io.EncoderFactory;
 import org.codehaus.jackson.JsonFactory;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -57,6 +67,30 @@ public class TestResolvingGrammarGenerat
     ResolvingGrammarGenerator.encode(e, schema, data);
     e.flush();
   }
+
+  @Test
+  public void testRecordMissingRequiredFieldError() throws Exception {
+    Schema schemaWithoutField = SchemaBuilder
+        .record("MyRecord").namespace("ns")
+        .fields()
+          .name("field1").type().stringType().noDefault()
+        .endRecord();
+    Schema schemaWithField = SchemaBuilder
+        .record("MyRecord").namespace("ns")
+        .fields()
+          .name("field1").type().stringType().noDefault()
+          .name("field2").type().stringType().noDefault()
+        .endRecord();
+    GenericData.Record record = new GenericRecordBuilder(schemaWithoutField).set("field1",
"someValue").build();
+    byte[] data = writeRecord(schemaWithoutField, record);
+    try {
+      readRecord(schemaWithField, data);
+      Assert.fail("Expected exception not thrown");
+    } catch (AvroTypeException typeException) {
+      Assert.assertEquals("Incorrect exception message",
+          "Found ns.MyRecord, expecting ns.MyRecord, missing required field field2", typeException.getMessage());
+    }
+  }
   
   @Parameterized.Parameters
   public static Collection<Object[]> data() {
@@ -92,4 +126,27 @@ public class TestResolvingGrammarGenerat
     return ret;
   }
 
+  private byte[] writeRecord(Schema schema, GenericData.Record record) throws Exception {
+    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+    GenericDatumWriter<GenericData.Record> datumWriter = new GenericDatumWriter<GenericData.Record>(schema);
+    DataFileWriter<GenericData.Record> writer = new DataFileWriter<GenericData.Record>(datumWriter);
+    try {
+      writer.create(schema, byteStream);
+      writer.append(record);
+    } finally {
+      writer.close();
+    }
+    return byteStream.toByteArray();
+  }
+
+  private GenericData.Record readRecord(Schema schema, byte[] data) throws Exception {
+    ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
+    GenericDatumReader<GenericData.Record> datumReader = new GenericDatumReader<GenericData.Record>(schema);
+    DataFileStream<GenericData.Record> reader = new DataFileStream<GenericData.Record>(byteStream,
datumReader);
+    try {
+      return reader.next();
+    } finally {
+      reader.close();
+    }
+  }
 }



Mime
View raw message