KAFKA-3631; Fix Struct.toString for nullable arrayOf Author: Grant Henke <[email protected]>
Reviewers: Ismael Juma <[email protected]> Closes #1279 from granthenke/struct-fix Project: http://git-wip-us.apache.org/repos/asf/kafka/repo Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/669be7fa Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/669be7fa Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/669be7fa Branch: refs/heads/0.10.0 Commit: 669be7fadc808a8436ba28d033b70d24641c75ea Parents: 94aee21 Author: Grant Henke <[email protected]> Authored: Wed Apr 27 17:15:37 2016 -0700 Committer: Ismael Juma <[email protected]> Committed: Wed Apr 27 17:15:37 2016 -0700 ---------------------------------------------------------------------- .../java/org/apache/kafka/common/protocol/types/Struct.java | 2 +- .../common/protocol/types/ProtocolSerializationTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kafka/blob/669be7fa/clients/src/main/java/org/apache/kafka/common/protocol/types/Struct.java ---------------------------------------------------------------------- diff --git a/clients/src/main/java/org/apache/kafka/common/protocol/types/Struct.java b/clients/src/main/java/org/apache/kafka/common/protocol/types/Struct.java index 7eee09f..212d701 100644 --- a/clients/src/main/java/org/apache/kafka/common/protocol/types/Struct.java +++ b/clients/src/main/java/org/apache/kafka/common/protocol/types/Struct.java @@ -290,7 +290,7 @@ public class Struct { Field f = this.schema.get(i); b.append(f.name); b.append('='); - if (f.type() instanceof ArrayOf) { + if (f.type() instanceof ArrayOf && this.values[i] != null) { Object[] arrayValue = (Object[]) this.values[i]; b.append('['); for (int j = 0; j < arrayValue.length; j++) { http://git-wip-us.apache.org/repos/asf/kafka/blob/669be7fa/clients/src/test/java/org/apache/kafka/common/protocol/types/ProtocolSerializationTest.java ---------------------------------------------------------------------- diff --git a/clients/src/test/java/org/apache/kafka/common/protocol/types/ProtocolSerializationTest.java b/clients/src/test/java/org/apache/kafka/common/protocol/types/ProtocolSerializationTest.java index e91b2fb..1633e89 100644 --- a/clients/src/test/java/org/apache/kafka/common/protocol/types/ProtocolSerializationTest.java +++ b/clients/src/test/java/org/apache/kafka/common/protocol/types/ProtocolSerializationTest.java @@ -18,6 +18,7 @@ package org.apache.kafka.common.protocol.types; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.nio.ByteBuffer; @@ -231,6 +232,13 @@ public class ProtocolSerializationTest { } } + @Test + public void testToString() { + String structStr = this.struct.toString(); + assertNotNull("Struct string should not be null.", structStr); + assertFalse("Struct string should not be empty.", structStr.isEmpty()); + } + private Object roundtrip(Type type, Object obj) { ByteBuffer buffer = ByteBuffer.allocate(type.sizeOf(obj)); type.write(buffer, obj);
