Skip to content

Commit 16f92d6

Browse files
Fixed bug when calling "getContent()" for SODA documents that do not
contain JSON.
1 parent ffdf962 commit 16f92d6

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

doc/src/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Thin Mode Changes
4848
Thick Mode Changes
4949
++++++++++++++++++
5050

51+
#) Fixed bug when calling :meth:`SodaDoc.getContent()` for SODA documents
52+
that do not contain JSON.
5153
#) Errors ``DPY-4011: the database or network closed the connection`` and
5254
``DPY-4024: call timeout of {timeout} ms exceeded`` now retain the original
5355
error message raised by the Oracle Client library.

src/oracledb/soda.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -----------------------------------------------------------------------------
2-
# Copyright (c) 2021, 2023, Oracle and/or its affiliates.
2+
# Copyright (c) 2021, 2024, Oracle and/or its affiliates.
33
#
44
# This software is dual-licensed to you under the Universal Permissive License
55
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
@@ -369,7 +369,10 @@ def getContent(self) -> Union[dict, list]:
369369
exception if this is not the case. If there is no content, however,
370370
None will be returned.
371371
"""
372-
return json.loads(self.getContentAsString())
372+
content_bytes, encoding = self._impl.get_content()
373+
if self.mediaType == "application/json":
374+
return json.loads(content_bytes.decode(encoding))
375+
return content_bytes
373376

374377
def getContentAsBytes(self) -> bytes:
375378
"""

tests/test_3400_soda_collection.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
3400 - Module for testing Simple Oracle Document Access (SODA) Collections
2727
"""
2828

29+
import json
2930
import unittest
3031

3132
import test_env
@@ -933,6 +934,42 @@ def test_3443(self):
933934
"TestSodaMapNonExistent", metadata=metadata, mapMode=True
934935
)
935936

937+
def test_3444(self):
938+
"3444 - test collections with mixture of media types"
939+
soda_db = self.get_soda_database()
940+
metadata = dict(mediaTypeColumn=dict(name="media_type"))
941+
coll = soda_db.createCollection("TestMixedMedia", metadata=metadata)
942+
test_data = [
943+
(dict(name="George", age=28), "application/json"),
944+
("Sample Text", "text/plain"),
945+
(b"\x57\x25\xfe\x34\x56", "application/octet-stream"),
946+
]
947+
for value, media_type in test_data:
948+
coll.find().remove()
949+
coll.insertOne(soda_db.createDocument(value, mediaType=media_type))
950+
fetched_doc = coll.find().getDocuments()[0]
951+
self.assertEqual(fetched_doc.mediaType, media_type)
952+
if media_type == "application/json":
953+
expected_content = value
954+
expected_str = json.dumps(value)
955+
expected_bytes = expected_str.encode()
956+
elif media_type == "text/plain":
957+
expected_content = expected_bytes = value.encode()
958+
expected_str = value
959+
else:
960+
expected_content = expected_bytes = value
961+
expected_str = None
962+
self.assertEqual(fetched_doc.getContent(), expected_content)
963+
self.assertEqual(fetched_doc.getContentAsBytes(), expected_bytes)
964+
if expected_str is None:
965+
self.assertRaises(
966+
UnicodeDecodeError, fetched_doc.getContentAsString
967+
)
968+
else:
969+
self.assertEqual(
970+
fetched_doc.getContentAsString(), expected_str
971+
)
972+
936973

937974
if __name__ == "__main__":
938975
test_env.run_test_cases()

0 commit comments

Comments
 (0)