From 80c53805004c4ad84efdd2181d88e265af6ede54 Mon Sep 17 00:00:00 2001 From: Marcello Steiner Date: Tue, 30 Oct 2018 09:45:49 +0100 Subject: [PATCH] Fixes googleapis/google-cloud-java#3808. Now getTable of BigQueryImpl checks if the provided TableId has the project set. If yes, the set projectId is used, otherwise the projectId used when creating the BigQuery client will be used. The additional test checks this behavior by calling getTableId with an empty projectId. --- .../com/google/cloud/bigquery/BigQueryImpl.java | 8 +++++++- .../google/cloud/bigquery/BigQueryImplTest.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index c4d6f0f7ae88..3cfaa9629609 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -39,6 +39,7 @@ import com.google.cloud.bigquery.spi.v2.BigQueryRpc; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; +import com.google.common.base.Strings; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -401,7 +402,12 @@ public Table getTable(final String datasetId, final String tableId, TableOption. @Override public Table getTable(TableId tableId, TableOption... options) { - final TableId completeTableId = tableId.setProjectId(getOptions().getProjectId()); + // More context about why this: https://github.com/googleapis/google-cloud-java/issues/3808 + final TableId completeTableId = tableId.setProjectId( + Strings.isNullOrEmpty(tableId.getProject()) + ? getOptions().getProjectId() + : tableId.getProject() + ); final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Table answer = diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index fe0d8572d46f..8291045b34d1 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -603,6 +603,20 @@ public void testGetTableFromTableIdWithProject() { assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); } + @Test + public void testGetTableFromTableIdWithoutProject() { + TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); + TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); + EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + .andReturn(tableInfo.toPb()); + EasyMock.replay(bigqueryRpcMock); + BigQueryOptions bigQueryOptions = + createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); + bigquery = bigQueryOptions.getService(); + Table table = bigquery.getTable(tableId); + assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); + } + @Test public void testGetTableWithSelectedFields() { Capture> capturedOptions = Capture.newInstance();