Skip to content

Commit

Permalink
Source BigQuery: Fix NPE when dataset_id isn't provided (#6051)
Browse files Browse the repository at this point in the history
* Source BigQuery: Fix for NPE when `dataset_id` isn't provided

* changes for an empty `dataset_id` in the configuration
* added changelog
* bumped version

* Source Bigquery: Version should be 0.1.2 🤦‍♂️

* Source BigQuery: Fixed formatting

* Fixes from `./gradlew format` run

* Source Bigquery: Bump version to 0.1.3
  • Loading branch information
69mb authored Sep 23, 2021
1 parent b4cca60 commit 9c5572b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"sourceDefinitionId": "bfd1ddf8-ae8a-4620-b1d7-55597d2ba08c",
"name": "BigQuery",
"dockerRepository": "airbyte/source-bigquery",
"dockerImageTag": "0.1.2",
"dockerImageTag": "0.1.3",
"documentationUrl": "https://docs.airbyte.io/integrations/sources/bigquery"
}
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@
- sourceDefinitionId: bfd1ddf8-ae8a-4620-b1d7-55597d2ba08c
name: BigQuery
dockerRepository: airbyte/source-bigquery
dockerImageTag: 0.1.2
dockerImageTag: 0.1.3
documentationUrl: https://docs.airbyte.io/integrations/sources/bigquery
sourceType: database
- sourceDefinitionId: 90916976-a132-4ce9-8bce-82a03dd58788
Expand Down
4 changes: 4 additions & 0 deletions airbyte-integrations/connectors/source-bigquery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog

## 0.1.2
Fix for NPE when optional `dataset_id` not provided in configuration.
2 changes: 1 addition & 1 deletion airbyte-integrations/connectors/source-bigquery/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ COPY build/distributions/${APPLICATION}*.tar ${APPLICATION}.tar
RUN tar xf ${APPLICATION}.tar --strip-components=1

# Airbyte's build system uses these labels to know what to name and tag the docker images produced by this Dockerfile.
LABEL io.airbyte.version=0.1.2
LABEL io.airbyte.version=0.1.3
LABEL io.airbyte.name=airbyte/source-bigquery
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ protected List<TableInfo<CommonField<StandardSQLTypeName>>> discoverInternal(Big
@Override
protected List<TableInfo<CommonField<StandardSQLTypeName>>> discoverInternal(BigQueryDatabase database, String schema) {
String projectId = dbConfig.get(CONFIG_PROJECT_ID).asText();
String datasetId = getConfigDatasetId(database);
List<Table> tables =
(isDatasetConfigured(database) ? database.getDatasetTables(getConfigDatasetId(database)) : database.getProjectTables(projectId));
List<TableInfo<CommonField<StandardSQLTypeName>>> result = new ArrayList<>();
tables.stream().map(table -> TableInfo.<CommonField<StandardSQLTypeName>>builder()
.nameSpace(datasetId)
.nameSpace(table.getTableId().getDataset())
.name(table.getTableId().getTable())
.fields(Objects.requireNonNull(table.getDefinition().getSchema()).getFields().stream()
.map(f -> {
Expand Down Expand Up @@ -177,11 +176,12 @@ private AutoCloseableIterator<JsonNode> queryTableWithParams(BigQueryDatabase da
}

private boolean isDatasetConfigured(SqlDatabase database) {
return database.getSourceConfig().hasNonNull(CONFIG_DATASET_ID);
JsonNode config = database.getSourceConfig();
return config.hasNonNull(CONFIG_DATASET_ID) ? !config.get(CONFIG_DATASET_ID).asText().isEmpty() : false;
}

private String getConfigDatasetId(SqlDatabase database) {
return (isDatasetConfigured(database) ? database.getSourceConfig().get(CONFIG_DATASET_ID).asText() : null);
return (isDatasetConfigured(database) ? database.getSourceConfig().get(CONFIG_DATASET_ID).asText() : "");
}

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2020 Airbyte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.airbyte.integrations.source.bigquery;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.JsonNode;
import io.airbyte.commons.json.Jsons;
import io.airbyte.commons.resources.MoreResources;
import java.io.IOException;
import org.junit.jupiter.api.Test;

class BigQuerySourceTest {

@Test
public void testEmptyDatasetIdInConfig() throws IOException {
JsonNode configJson = Jsons.deserialize(MoreResources.readResource("test_config_empty_datasetid.json"));
JsonNode dbConfig = new BigQuerySource().toDatabaseConfig(configJson);
assertTrue(dbConfig.get(BigQuerySource.CONFIG_DATASET_ID).isEmpty());
}

@Test
public void testConfig() throws IOException {
JsonNode configJson = Jsons.deserialize(MoreResources.readResource("test_config.json"));
JsonNode dbConfig = new BigQuerySource().toDatabaseConfig(configJson);
assertEquals("dataset", dbConfig.get(BigQuerySource.CONFIG_DATASET_ID).asText());
assertEquals("project", dbConfig.get(BigQuerySource.CONFIG_PROJECT_ID).asText());
assertEquals("credentials", dbConfig.get(BigQuerySource.CONFIG_CREDS).asText());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dataset_id": "dataset",
"project_id": "project",
"credentials_json": "credentials"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dataset_id": "",
"project_id": "xxxx",
"credentials_json": "xxxx"
}

0 comments on commit 9c5572b

Please sign in to comment.