Skip to content

Commit

Permalink
Fix deserializing None QueryParameters (googleapis#9029)
Browse files Browse the repository at this point in the history
For None parameters, the back-end does not return the parameter
value in response. This commit adjusts the ScalarQueryParameter's
method from_api_repr().
  • Loading branch information
plamut authored and emar-kar committed Sep 18, 2019
1 parent 0523a98 commit 9c22988
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions bigquery/google/cloud/bigquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,15 @@ def from_api_repr(cls, resource):
"""
name = resource.get("name")
type_ = resource["parameterType"]["type"]
value = resource["parameterValue"]["value"]
converted = _QUERY_PARAMS_FROM_JSON[type_](value, None)

# parameterValue might not be present if JSON resource originates
# from the back-end - the latter omits it for None values.
value = resource.get("parameterValue", {}).get("value")
if value is not None:
converted = _QUERY_PARAMS_FROM_JSON[type_](value, None)
else:
converted = None

return cls(name, type_, converted)

def to_api_repr(self):
Expand Down
9 changes: 9 additions & 0 deletions bigquery/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ def test_from_api_repr_wo_name(self):
self.assertEqual(param.type_, "INT64")
self.assertEqual(param.value, 123)

def test_from_api_repr_wo_value(self):
# Back-end may not send back values for None params. See #9027
RESOURCE = {"name": "foo", "parameterType": {"type": "INT64"}}
klass = self._get_target_class()
param = klass.from_api_repr(RESOURCE)
self.assertEqual(param.name, "foo")
self.assertEqual(param.type_, "INT64")
self.assertIs(param.value, None)

def test_to_api_repr_w_name(self):
EXPECTED = {
"name": "foo",
Expand Down

0 comments on commit 9c22988

Please sign in to comment.