Skip to content

Commit

Permalink
fix: if JsonObject serialized to None then return null_value instea…
Browse files Browse the repository at this point in the history
…d of `string_value` (#771)

* fix: if JsonObject serialized to None then return `null_value` instead of `string_value`

Co-authored-by: Astha Mohta <35952883+asthamohta@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
Co-authored-by: Ilya Gurov <gurovilya9@gmail.com>
  • Loading branch information
4 people committed Aug 25, 2022
1 parent def00a8 commit 82170b5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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

0 comments on commit 82170b5

Please sign in to comment.