Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if JsonObject serialized to None then return null_value instead of string_value #771

Merged
merged 8 commits into from
Aug 25, 2022
6 changes: 5 additions & 1 deletion google/cloud/spanner_v1/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ def _make_value_pb(value):
_assert_numeric_precision_and_scale(value)
return Value(string_value=str(value))
if isinstance(value, JsonObject):
return Value(string_value=value.serialize())
value = value.serialize()
if value is None:
return Value(null_value="NULL_VALUE")
else:
return Value(string_value=value)

raise ValueError("Unknown type: %s" % (value,))

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ def test_w_json(self):
self.assertIsInstance(value_pb, Value)
self.assertEqual(value_pb.string_value, value)

def test_w_json_None(self):
from google.cloud.spanner_v1 import JsonObject

value = JsonObject(None)
value_pb = self._callFUT(value)
self.assertTrue(value_pb.HasField("null_value"))


class Test_make_list_value_pb(unittest.TestCase):
def _callFUT(self, *args, **kw):
Expand Down