Skip to content

Commit fac7355

Browse files
committed
Added automatic conversion of UUIDs to strings.
1 parent 05159b8 commit fac7355

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ htmlcov/
1919
out/
2020
parts/
2121
tmp/
22+
env/

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Changes for crate
55
Unreleased
66
==========
77

8+
- Properly handle Python-native UUID types in SQL parameters
89

910
2023/07/17 0.33.0
1011
=================

src/crate/client/http.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
from time import time
3636
from datetime import datetime, date, timezone
3737
from decimal import Decimal
38+
from uuid import UUID
39+
3840
from urllib3 import connection_from_url
3941
from urllib3.connection import HTTPConnection
4042
from urllib3.exceptions import (
@@ -86,7 +88,7 @@ class CrateJsonEncoder(json.JSONEncoder):
8688
epoch_naive = datetime(1970, 1, 1)
8789

8890
def default(self, o):
89-
if isinstance(o, Decimal):
91+
if isinstance(o, (Decimal, UUID)):
9092
return str(o)
9193
if isinstance(o, datetime):
9294
if o.tzinfo is not None:

src/crate/client/test_http.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
from threading import Thread, Event
3636
from decimal import Decimal
3737
import datetime as dt
38+
3839
import urllib3.exceptions
3940
from base64 import b64decode
4041
from urllib.parse import urlparse, parse_qs
42+
43+
import uuid
4144
from setuptools.ssl_support import find_ca_bundle
4245

4346
from .http import Client, CrateJsonEncoder, _get_socket_opts, _remove_certs_for_non_https
@@ -287,6 +290,18 @@ def test_socket_options_contain_keepalive(self):
287290
)
288291
client.close()
289292

293+
@patch(REQUEST, autospec=True)
294+
def test_uuid_serialization(self, request):
295+
client = Client(servers="localhost:4200")
296+
request.return_value = fake_response(200)
297+
298+
uid = uuid.uuid4()
299+
client.sql('insert into my_table (str_col) values (?)', (uid,))
300+
301+
data = json.loads(request.call_args[1]['data'])
302+
self.assertEqual(data['args'], [str(uid)])
303+
client.close()
304+
290305

291306
@patch(REQUEST, fail_sometimes)
292307
class ThreadSafeHttpClientTest(TestCase):

0 commit comments

Comments
 (0)