Skip to content

fix: Issue#1154 Improper initialisation of JsonObject when called on JsonObject of list #1166

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

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)