avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r1674398 - 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 Fri, 17 Apr 2015 20:47:13 GMT
Author: blue
Date: Fri Apr 17 20:47:13 2015
New Revision: 1674398

URL: http://svn.apache.org/r1674398
Log:
AVRO-1655. Java: Fix NPE in RecordSchema#toString. Contributed by Lars Francke.

This also adds a variant of Schema.createRecord with a field list to
allow callers to create valid record schemas in a single call.

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=1674398&r1=1674397&r2=1674398&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Apr 17 20:47:13 2015
@@ -68,6 +68,9 @@ Trunk (not yet released)
     AVRO-1628. Java: Add Schema#createUnion(Schema ...) convenience method.
     (Clément Mahtieu via cutting)
 
+    AVRO-1655. Java: Add Schema.createRecord with field list.
+    (Lars Francke via blue)
+
   BUG FIXES
 
     AVRO-1553. Java: MapReduce never uses MapOutputValueSchema (tomwhite)
@@ -131,6 +134,9 @@ Trunk (not yet released)
     AVRO-1652. Java: Do not warn or validate defaults if validation is off.
     (Michael D'Angelo via blue)
 
+    AVRO-1655. Java: Fix NPE in RecordSchema#toString when fields are null.
+    (Lars Francke via blue)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

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=1674398&r1=1674397&r2=1674398&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 Fri Apr 17 20:47:13
2015
@@ -151,6 +151,12 @@ public abstract class Schema extends Jso
     return new RecordSchema(new Name(name, namespace), doc, isError);
   }
 
+  /** Create a named record schema with fields already set. */
+  public static Schema createRecord(String name, String doc, String namespace,
+                                    boolean isError, List<Field> fields) {
+    return new RecordSchema(new Name(name, namespace), doc, isError, fields);
+  }
+
   /** Create an enum schema. */
   public static Schema createEnum(String name, String doc, String namespace,
                                   List<String> values) {
@@ -594,6 +600,14 @@ public abstract class Schema extends Jso
       super(Type.RECORD, name, doc);
       this.isError = isError;
     }
+
+    public RecordSchema(Name name, String doc, boolean isError,
+                        List<Field> fields) {
+      super(Type.RECORD, name, doc);
+      this.isError = isError;
+      setFields(fields);
+    }
+
     public boolean isError() { return isError; }
 
     @Override
@@ -671,8 +685,12 @@ public abstract class Schema extends Jso
       names.space = name.space;                   // set default namespace
       if (getDoc() != null)
         gen.writeStringField("doc", getDoc());
-      gen.writeFieldName("fields");
-      fieldsToJson(names, gen);
+
+      if (fields != null) {
+        gen.writeFieldName("fields");
+        fieldsToJson(names, gen);
+      }
+
       writeProps(gen);
       aliasesToJson(gen);
       gen.writeEndObject();

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=1674398&r1=1674397&r2=1674398&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 Fri Apr 17 20:47:13
2015
@@ -75,4 +75,29 @@ public class TestSchema {
     Schema schema = Schema.createUnion(Schema.create(Type.NULL), Schema.create(Type.LONG));
     assertEquals(expected, schema);
   }
+
+  @Test
+  public void testEmptyRecordSchema() {
+    Schema schema = Schema.createRecord("foobar", null, null, false);
+    String schemaString = schema.toString();
+    assertNotNull(schemaString);
+  }
+
+  @Test
+  public void testSchemaWithFields() {
+    List<Field> fields = new ArrayList<Field>();
+    fields.add(new Field("field_name1", Schema.create(Type.NULL), null, null));
+    fields.add(new Field("field_name2", Schema.create(Type.INT), null, null));
+    Schema schema = Schema.createRecord("foobar", null, null, false, fields);
+    String schemaString = schema.toString();
+    assertNotNull(schemaString);
+    assertEquals(2, schema.getFields().size());
+  }
+
+  @Test(expected = NullPointerException.class)
+  public void testSchemaWithNullFields() {
+    Schema.createRecord("foobar", null, null, false, null);
+  }
+
+
 }



Mime
View raw message