avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1515957 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/Schema.java lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
Date Tue, 20 Aug 2013 20:26:13 GMT
Author: cutting
Date: Tue Aug 20 20:26:13 2013
New Revision: 1515957

URL: http://svn.apache.org/r1515957
Log:
AVRO-1355. Java: Reject schemas with duplicate field names.  Contributed by Christophe Taton.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1515957&r1=1515956&r2=1515957&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Aug 20 20:26:13 2013
@@ -8,6 +8,9 @@ Trunk (not yet released)
 
   IMPROVEMENTS
 
+    AVRO-1355. Java: Reject schemas with duplicate field
+    names. (Christophe Taton via cutting)
+
   BUG FIXES
 
 Avro 1.7.5 (12 August 2013)

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java?rev=1515957&r1=1515956&r2=1515957&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java Tue Aug 20 20:26:13
2013
@@ -587,7 +587,12 @@ public abstract class Schema extends Jso
         if (f.position != -1)
           throw new AvroRuntimeException("Field already used: " + f);
         f.position = i++;
-        fieldMap.put(f.name(), f);
+        final Field existingField = fieldMap.put(f.name(), f);
+        if (existingField != null) {
+          throw new AvroRuntimeException(String.format(
+              "Duplicate field %s in record %s: %s and %s.",
+              f.name(), name, f, existingField));
+        }
         ff.add(f);
       }
       this.fields = ff.lock();

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java?rev=1515957&r1=1515956&r2=1515957&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java Tue Aug 20 20:26:13
2013
@@ -19,7 +19,14 @@ package org.apache.avro;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.avro.Schema.Field;
+import org.apache.avro.Schema.Type;
 import org.junit.Test;
 
 public class TestSchema {  
@@ -43,4 +50,18 @@ public class TestSchema {  
     assertNotNull(parsedArrayOfStringSchema);
     assertEquals(parsedStringSchema.toString(), parsedArrayOfStringSchema.toString());
   }
+
+  @Test
+  public void testDuplicateRecordFieldName() {
+    final Schema schema = Schema.createRecord("RecordName", null, null, false);
+    final List<Field> fields = new ArrayList<Field>();
+    fields.add(new Field("field_name", Schema.create(Type.NULL), null, null));
+    fields.add(new Field("field_name", Schema.create(Type.INT), null, null));
+    try {
+      schema.setFields(fields);
+      fail("Should not be able to create a record with duplicate field name.");
+    } catch (AvroRuntimeException are) {
+      assertTrue(are.getMessage().contains("Duplicate field field_name in record RecordName"));
+    }
+  }
 }



Mime
View raw message