From dd6ebea11fe8c50a70bb222608f0aac2db0eaf6a Mon Sep 17 00:00:00 2001 From: Nikola Taushanov Date: Sat, 22 Dec 2018 17:13:42 +0200 Subject: [PATCH] Fix NPE in BQ's StandardTableDefinition --- .../bigquery/StandardTableDefinition.java | 19 +++++++++++++------ .../bigquery/StandardTableDefinitionTest.java | 5 +++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java index 880372b2b733..f12e6b608a91 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java @@ -57,12 +57,12 @@ public static class StreamingBuffer implements Serializable { } /** Returns a lower-bound estimate of the number of rows currently in the streaming buffer. */ - public long getEstimatedRows() { + public Long getEstimatedRows() { return estimatedRows; } /** Returns a lower-bound estimate of the number of bytes currently in the streaming buffer. */ - public long getEstimatedBytes() { + public Long getEstimatedBytes() { return estimatedBytes; } @@ -95,10 +95,17 @@ public boolean equals(Object obj) { } Streamingbuffer toPb() { - return new Streamingbuffer() - .setEstimatedBytes(BigInteger.valueOf(estimatedBytes)) - .setEstimatedRows(BigInteger.valueOf(estimatedRows)) - .setOldestEntryTime(BigInteger.valueOf(oldestEntryTime)); + Streamingbuffer buffer = new Streamingbuffer(); + if (estimatedBytes != null) { + buffer.setEstimatedBytes(BigInteger.valueOf(estimatedBytes)); + } + if (estimatedRows != null) { + buffer.setEstimatedRows(BigInteger.valueOf(estimatedRows)); + } + if (oldestEntryTime != null) { + buffer.setOldestEntryTime(BigInteger.valueOf(oldestEntryTime)); + } + return buffer; } static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) { diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java index 79414fa5e6c2..d70d33fb119b 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java @@ -120,6 +120,11 @@ public void testFromPbWithNullEstimatedRowsAndBytes() { TABLE_DEFINITION.toPb().setStreamingBuffer(new Streamingbuffer())); } + @Test + public void testStreamingBufferWithNullFieldsToPb() { + new StreamingBuffer(null, null, null).toPb(); + } + private void compareStandardTableDefinition( StandardTableDefinition expected, StandardTableDefinition value) { assertEquals(expected, value);