Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4251b19

Browse files
authoredSep 20, 2021
fix: do not throw NPE from BigQueryImpl.testIamPermissions (#1596)
Fix for issue where `BigQueryImpl.testIamPermissions` will throw a NullPointerException if the caller does not have any of the permissions being checked. Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [X] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [X] Appropriate docs were updated (if necessary) Fixes #1595 ☕️
1 parent e384f5a commit 4251b19

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
 

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,9 @@ public com.google.api.services.bigquery.model.TestIamPermissionsResponse call()
14951495
getOptions().getRetrySettings(),
14961496
EXCEPTION_HANDLER,
14971497
getOptions().getClock());
1498-
return ImmutableList.copyOf(response.getPermissions());
1498+
return response.getPermissions() == null
1499+
? ImmutableList.of()
1500+
: ImmutableList.copyOf(response.getPermissions());
14991501
} catch (RetryHelperException e) {
15001502
throw BigQueryException.translateAndThrow(e);
15011503
}

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -2766,4 +2766,21 @@ public void testTestIamPermissions() {
27662766
assertEquals(perms, grantedPermissions);
27672767
verify(bigqueryRpcMock).testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS);
27682768
}
2769+
2770+
@Test
2771+
public void testTestIamPermissionsWhenNoPermissionsGranted() {
2772+
final String resourceId =
2773+
String.format("projects/%s/datasets/%s/tables/%s", PROJECT, DATASET, TABLE);
2774+
final List<String> checkedPermissions = ImmutableList.<String>of("foo", "bar", "baz");
2775+
// If caller has no permissions, TestIamPermissionsResponse.permissions will be null
2776+
final com.google.api.services.bigquery.model.TestIamPermissionsResponse response =
2777+
new com.google.api.services.bigquery.model.TestIamPermissionsResponse()
2778+
.setPermissions(null);
2779+
when(bigqueryRpcMock.testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS))
2780+
.thenReturn(response);
2781+
bigquery = options.getService();
2782+
List<String> perms = bigquery.testIamPermissions(TABLE_ID, checkedPermissions);
2783+
assertEquals(perms, ImmutableList.of());
2784+
verify(bigqueryRpcMock).testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS);
2785+
}
27692786
}

0 commit comments

Comments
 (0)
Failed to load comments.