You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by ni...@apache.org on 2017/12/22 22:38:11 UTC

[2/2] avro git commit: AVRO-1966: Java: Fix NPE When copying builder with nullable record.

AVRO-1966: Java: Fix NPE When copying builder with nullable record.


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/8bed58c6
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/8bed58c6
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/8bed58c6

Branch: refs/heads/branch-1.8
Commit: 8bed58c6ca57f43c20d2c0eae30e0d4e82d5e6fc
Parents: d35724e
Author: Niels Basjes <ni...@apache.org>
Authored: Wed Nov 30 21:50:13 2016 +0100
Committer: Niels Basjes <nb...@bol.com>
Committed: Fri Dec 22 23:29:48 2017 +0100

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../specific/templates/java/classic/record.vm   |  2 +-
 .../avro/specific/TestSpecificBuilderTree.java  | 29 ++++++++++++++
 .../avro/examples/baseball/Player.java          |  8 ++--
 .../tools/src/test/compiler/output/Player.java  |  8 ++--
 share/test/schemas/nestedNullable.avdl          | 41 ++++++++++++++++++++
 6 files changed, 81 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/8bed58c6/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index d542d30..ff4e51f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -28,6 +28,8 @@ Trunk (not yet released)
     AVRO-1485: Specification says Record field type can be record name but implementation allows any named type.
     (Nandor Kollar via gabor)
 
+    AVRO-1966: Java: Fix NPE When copying builder with nullable record. (Niels Basjes)
+
 Avro 1.8.2 (10 April 2017)
 
   INCOMPATIBLE CHANGES

http://git-wip-us.apache.org/repos/asf/avro/blob/8bed58c6/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
----------------------------------------------------------------------
diff --git a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
index 94090d8..50c9ecf 100644
--- a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
+++ b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
@@ -274,7 +274,7 @@ public class ${this.mangle($schema.getName())}#if ($schema.isError()) extends or
 #foreach ($field in $schema.getFields())
       if (isValidValue(fields()[$field.pos()], other.${this.mangle($field.name(), $schema.isError())})) {
         this.${this.mangle($field.name(), $schema.isError())} = data().deepCopy(fields()[$field.pos()].schema(), other.${this.mangle($field.name(), $schema.isError())});
-        fieldSetFlags()[$field.pos()] = true;
+        fieldSetFlags()[$field.pos()] = other.fieldSetFlags()[$field.pos()];
       }
 #if (${this.hasBuilder($field.schema())})
       if (other.${this.generateHasBuilderMethod($schema, $field)}()) {

http://git-wip-us.apache.org/repos/asf/avro/blob/8bed58c6/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificBuilderTree.java
----------------------------------------------------------------------
diff --git a/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificBuilderTree.java b/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificBuilderTree.java
index 24afd52..f9856e9 100644
--- a/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificBuilderTree.java
+++ b/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificBuilderTree.java
@@ -19,11 +19,15 @@ package org.apache.avro.specific;
 
 import org.apache.avro.AvroMissingFieldException;
 import org.apache.avro.test.http.*;
+import org.apache.avro.test.nullable.Nullable;
+import org.apache.avro.test.nullable.RecordWithNullables;
 import org.junit.Test;
 
 import java.util.ArrayList;
 
+import static org.apache.avro.test.nullable.Nullable.*;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
 
 public class TestSpecificBuilderTree {
@@ -260,4 +264,29 @@ public class TestSpecificBuilderTree {
     assertEquals("/index.html",     request.getHttpRequest().getURI().getPath());
   }
 
+  @Test
+  public void copyBuilderWithNullables() {
+    RecordWithNullables.Builder builder = RecordWithNullables.newBuilder();
+
+    assertFalse(builder.hasNullableRecordBuilder());
+    assertFalse(builder.hasNullableRecord());
+    assertFalse(builder.hasNullableString());
+    assertFalse(builder.hasNullableLong  ());
+    assertFalse(builder.hasNullableInt   ());
+    assertFalse(builder.hasNullableMap   ());
+    assertFalse(builder.hasNullableArray ());
+
+    RecordWithNullables.Builder builderCopy = RecordWithNullables.newBuilder(builder);
+
+    assertFalse(builderCopy.hasNullableRecordBuilder());
+    assertFalse(builderCopy.hasNullableRecord());
+    assertFalse(builderCopy.hasNullableString());
+    assertFalse(builderCopy.hasNullableLong  ());
+    assertFalse(builderCopy.hasNullableInt   ());
+    assertFalse(builderCopy.hasNullableMap   ());
+    assertFalse(builderCopy.hasNullableArray ());
+
+    builderCopy.getNullableRecordBuilder();
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/avro/blob/8bed58c6/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
----------------------------------------------------------------------
diff --git a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
index e8abfc7..77386d2 100644
--- a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
+++ b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
@@ -229,19 +229,19 @@ public class Player extends org.apache.avro.specific.SpecificRecordBase implemen
       super(other);
       if (isValidValue(fields()[0], other.number)) {
         this.number = data().deepCopy(fields()[0].schema(), other.number);
-        fieldSetFlags()[0] = true;
+        fieldSetFlags()[0] = other.fieldSetFlags()[0];
       }
       if (isValidValue(fields()[1], other.first_name)) {
         this.first_name = data().deepCopy(fields()[1].schema(), other.first_name);
-        fieldSetFlags()[1] = true;
+        fieldSetFlags()[1] = other.fieldSetFlags()[1];
       }
       if (isValidValue(fields()[2], other.last_name)) {
         this.last_name = data().deepCopy(fields()[2].schema(), other.last_name);
-        fieldSetFlags()[2] = true;
+        fieldSetFlags()[2] = other.fieldSetFlags()[2];
       }
       if (isValidValue(fields()[3], other.position)) {
         this.position = data().deepCopy(fields()[3].schema(), other.position);
-        fieldSetFlags()[3] = true;
+        fieldSetFlags()[3] = other.fieldSetFlags()[3];
       }
     }
 

http://git-wip-us.apache.org/repos/asf/avro/blob/8bed58c6/lang/java/tools/src/test/compiler/output/Player.java
----------------------------------------------------------------------
diff --git a/lang/java/tools/src/test/compiler/output/Player.java b/lang/java/tools/src/test/compiler/output/Player.java
index eb0e2f2..b20d55b 100644
--- a/lang/java/tools/src/test/compiler/output/Player.java
+++ b/lang/java/tools/src/test/compiler/output/Player.java
@@ -229,19 +229,19 @@ public class Player extends org.apache.avro.specific.SpecificRecordBase implemen
       super(other);
       if (isValidValue(fields()[0], other.number)) {
         this.number = data().deepCopy(fields()[0].schema(), other.number);
-        fieldSetFlags()[0] = true;
+        fieldSetFlags()[0] = other.fieldSetFlags()[0];
       }
       if (isValidValue(fields()[1], other.first_name)) {
         this.first_name = data().deepCopy(fields()[1].schema(), other.first_name);
-        fieldSetFlags()[1] = true;
+        fieldSetFlags()[1] = other.fieldSetFlags()[1];
       }
       if (isValidValue(fields()[2], other.last_name)) {
         this.last_name = data().deepCopy(fields()[2].schema(), other.last_name);
-        fieldSetFlags()[2] = true;
+        fieldSetFlags()[2] = other.fieldSetFlags()[2];
       }
       if (isValidValue(fields()[3], other.position)) {
         this.position = data().deepCopy(fields()[3].schema(), other.position);
-        fieldSetFlags()[3] = true;
+        fieldSetFlags()[3] = other.fieldSetFlags()[3];
       }
     }
 

http://git-wip-us.apache.org/repos/asf/avro/blob/8bed58c6/share/test/schemas/nestedNullable.avdl
----------------------------------------------------------------------
diff --git a/share/test/schemas/nestedNullable.avdl b/share/test/schemas/nestedNullable.avdl
new file mode 100644
index 0000000..a62c205
--- /dev/null
+++ b/share/test/schemas/nestedNullable.avdl
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@namespace("org.apache.avro.test.nullable")
+protocol Nullable {
+
+    enum MyEnum {
+        One,
+        Two
+    }
+
+    record SubRecord {
+        string value;
+    }
+
+    record RecordWithNullables {
+        union { null, string        } nullableString    = null;
+        union { null, long          } nullableLong      = null;
+        union { null, int           } nullableInt       = null;
+        union { null, map<string>   } nullableMap       = null;
+        union { null, array<string> } nullableArray     = null;
+        union { null, SubRecord     } nullableRecord    = null;
+        union { null, MyEnum        } nullableEnum      = null;
+    }
+
+}