Repository: avro Updated Branches: refs/heads/master cae491a62 -> a53a4fd10
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/a53a4fd1 Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/a53a4fd1 Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/a53a4fd1 Branch: refs/heads/master Commit: a53a4fd10072471aa7c8170499a10792e61e0d95 Parents: cae491a Author: Niels Basjes <nielsbas...@apache.org> Authored: Wed Nov 30 21:50:13 2016 +0100 Committer: Niels Basjes <nielsbas...@apache.org> Committed: Wed Nov 30 21:50:13 2016 +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/a53a4fd1/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 3553e2a..fa11bb8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -104,6 +104,8 @@ Trunk (not yet released) AVRO-1951: Python: Fix ipc_test.py with a valid DNS name. (blue) + AVRO-1966: Java: Fix NPE When copying builder with nullable record. (Niels Basjes) + Avro 1.8.1 (14 May 2016) INCOMPATIBLE CHANGES http://git-wip-us.apache.org/repos/asf/avro/blob/a53a4fd1/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 85c5e9d..c333dd0 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 @@ -264,7 +264,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/a53a4fd1/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 a0ad619..87b9e6f 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 @@ -18,11 +18,15 @@ package org.apache.avro.specific; 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 { @@ -254,4 +258,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/a53a4fd1/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 ffd083b..da58e20 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 @@ -219,19 +219,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/a53a4fd1/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 ce8c84c..0a0b882 100644 --- a/lang/java/tools/src/test/compiler/output/Player.java +++ b/lang/java/tools/src/test/compiler/output/Player.java @@ -219,19 +219,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/a53a4fd1/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; + } + +}