File tree Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -352,6 +352,13 @@ def close(self) -> None:
352352 self ._active_sessions .clear ()
353353 self ._idle_sessions .clear ()
354354
355+ # Customizable JSONEncoder to support additional types.
356+ class SnowflakeRestfulJsonEncoder (json .JSONEncoder ):
357+ def default (self , o ):
358+ if isinstance (o , uuid .UUID ):
359+ return str (o )
360+
361+ return super ().default (o )
355362
356363class SnowflakeRestful :
357364 """Snowflake Restful class."""
@@ -499,7 +506,7 @@ def request(
499506 return self ._post_request (
500507 url ,
501508 headers ,
502- json .dumps (body ),
509+ json .dumps (body , cls = SnowflakeRestfulJsonEncoder ),
503510 token = self .token ,
504511 _no_results = _no_results ,
505512 timeout = timeout ,
@@ -561,7 +568,7 @@ def _token_request(self, request_type):
561568 ret = self ._post_request (
562569 url ,
563570 headers ,
564- json .dumps (body ),
571+ json .dumps (body , cls = SnowflakeRestfulJsonEncoder ),
565572 token = header_token ,
566573 )
567574 if ret .get ("success" ) and ret .get ("data" , {}).get ("sessionToken" ):
@@ -659,7 +666,7 @@ def delete_session(self, retry: bool = False) -> None:
659666 ret = self ._post_request (
660667 url ,
661668 headers ,
662- json .dumps (body ),
669+ json .dumps (body , cls = SnowflakeRestfulJsonEncoder ),
663670 token = self .token ,
664671 timeout = 5 ,
665672 no_retry = True ,
Original file line number Diff line number Diff line change 11#!/usr/bin/env python
22import io
3+ import json
34import unittest .mock
5+ import uuid
6+
7+ from src .snowflake .connector .network import SnowflakeRestfulJsonEncoder
48from test .unit .mock_utils import mock_connection
59
610import pytest
@@ -63,3 +67,14 @@ def test_fetch():
6367 # if no retry is set to False, the function raises an InterfaceError
6468 with pytest .raises (InterfaceError ) as exc :
6569 assert rest .fetch (** default_parameters , no_retry = False )
70+
71+ @pytest .mark .parametrize ("u" , [uuid .uuid1 (),
72+ uuid .uuid3 (uuid .NAMESPACE_URL , "www.snowflake.com" ),
73+ uuid .uuid4 (),
74+ uuid .uuid5 (uuid .NAMESPACE_URL , "www.snowflake.com" )])
75+ def test_json_serialize_uuid (u ):
76+ expected = f"{{\" u\" : \" { u } \" , \" a\" : 42}}"
77+
78+ assert (json .dumps (u , cls = SnowflakeRestfulJsonEncoder )) == f"\" { u } \" "
79+
80+ assert json .dumps ({"u" :u , "a" :42 }, cls = SnowflakeRestfulJsonEncoder ) == expected
You can’t perform that action at this time.
0 commit comments