Skip to content

Commit

Permalink
fix: JsonObject init when called on JsonObject of list (googleapis#1166)
Browse files Browse the repository at this point in the history
Co-authored-by: Sri Harsha CH <[email protected]>
  • Loading branch information
forksumit and harshachinta authored Aug 27, 2024
1 parent f886ebd commit c4af6f0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions google/cloud/spanner_v1/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def __init__(self, *args, **kwargs):
self._array_value = args[0]
return

if len(args) and isinstance(args[0], JsonObject):
self._is_array = args[0]._is_array
if self._is_array:
self._array_value = args[0]._array_value

if not self._is_null:
super(JsonObject, self).__init__(*args, **kwargs)

Expand Down
45 changes: 45 additions & 0 deletions tests/unit/test_datatypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2024 Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest

import json
from google.cloud.spanner_v1.data_types import JsonObject


class Test_JsonObject_serde(unittest.TestCase):
def test_w_dict(self):
data = {"foo": "bar"}
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
data_jsonobject = JsonObject(data)
self.assertEqual(data_jsonobject.serialize(), expected)

def test_w_list_of_dict(self):
data = [{"foo1": "bar1"}, {"foo2": "bar2"}]
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
data_jsonobject = JsonObject(data)
self.assertEqual(data_jsonobject.serialize(), expected)

def test_w_JsonObject_of_dict(self):
data = {"foo": "bar"}
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
data_jsonobject = JsonObject(JsonObject(data))
self.assertEqual(data_jsonobject.serialize(), expected)

def test_w_JsonObject_of_list_of_dict(self):
data = [{"foo1": "bar1"}, {"foo2": "bar2"}]
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
data_jsonobject = JsonObject(JsonObject(data))
self.assertEqual(data_jsonobject.serialize(), expected)

0 comments on commit c4af6f0

Please sign in to comment.