Skip to content

Commit

Permalink
Harden 'ArrayQueryParameter.from_api_repr' against missing 'parameter…
Browse files Browse the repository at this point in the history
…Value'. (#7311)

Closes #7309.
  • Loading branch information
tseaver authored Feb 12, 2019
1 parent 4d86666 commit b360a63
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bigquery/google/cloud/bigquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def _from_api_repr_struct(cls, resource):
def _from_api_repr_scalar(cls, resource):
name = resource.get("name")
array_type = resource["parameterType"]["arrayType"]["type"]
values = [value["value"] for value in resource["parameterValue"]["arrayValues"]]
parameter_value = resource.get("parameterValue", {})
array_values = parameter_value.get("arrayValues", ())
values = [value["value"] for value in array_values]
converted = [
_QUERY_PARAMS_FROM_JSON[array_type](value, None) for value in values
]
Expand Down
12 changes: 12 additions & 0 deletions bigquery/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,18 @@ def test_from_api_repr_wo_name(self):
self.assertEqual(param.array_type, "INT64")
self.assertEqual(param.values, [1, 2])

def test_from_api_repr_wo_values(self):
# Back-end may not send back values for empty array params. See #7309
RESOURCE = {
"name": "foo",
"parameterType": {"type": "ARRAY", "arrayType": {"type": "INT64"}},
}
klass = self._get_target_class()
param = klass.from_api_repr(RESOURCE)
self.assertEqual(param.name, "foo")
self.assertEqual(param.array_type, "INT64")
self.assertEqual(param.values, [])

def test_from_api_repr_w_struct_type(self):
from google.cloud.bigquery.query import StructQueryParameter

Expand Down

0 comments on commit b360a63

Please sign in to comment.