From e5e72be1e073654037bf4b587727eb35a032e633 Mon Sep 17 00:00:00 2001 From: Ryan Culbertson Date: Mon, 20 Sep 2021 15:35:48 -0400 Subject: [PATCH] fix: do not throw NPE from BigQueryImpl.testIamPermissions Fix for issue where BigQueryImpl.testIamPermissions will throw a NullPointerException if the caller does not have any of the permissions being checked. --- .../com/google/cloud/bigquery/BigQueryImpl.java | 4 +++- .../google/cloud/bigquery/BigQueryImplTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 03635a89e..fd9160f40 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -1495,7 +1495,9 @@ public com.google.api.services.bigquery.model.TestIamPermissionsResponse call() getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); - return ImmutableList.copyOf(response.getPermissions()); + return response.getPermissions() == null + ? ImmutableList.of() + : ImmutableList.copyOf(response.getPermissions()); } catch (RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index a12800c87..760c84f32 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -2766,4 +2766,21 @@ public void testTestIamPermissions() { assertEquals(perms, grantedPermissions); verify(bigqueryRpcMock).testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS); } + + @Test + public void testTestIamPermissionsWhenNoPermissionsGranted() { + final String resourceId = + String.format("projects/%s/datasets/%s/tables/%s", PROJECT, DATASET, TABLE); + final List checkedPermissions = ImmutableList.of("foo", "bar", "baz"); + // If caller has no permissions, TestIamPermissionsResponse.permissions will be null + final com.google.api.services.bigquery.model.TestIamPermissionsResponse response = + new com.google.api.services.bigquery.model.TestIamPermissionsResponse() + .setPermissions(null); + when(bigqueryRpcMock.testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS)) + .thenReturn(response); + bigquery = options.getService(); + List perms = bigquery.testIamPermissions(TABLE_ID, checkedPermissions); + assertEquals(perms, ImmutableList.of()); + verify(bigqueryRpcMock).testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS); + } }