From ed8f17923f90f7ac91315feb69506436d26a779a Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Mar 14 2018 09:54:27 +0000 Subject: [PATCH 1/2] json serialize additional types in protonmsg Fixes: https://pagure.io/koji/issue/741 --- diff --git a/plugins/hub/protonmsg.py b/plugins/hub/protonmsg.py index 408e368..134b42d 100644 --- a/plugins/hub/protonmsg.py +++ b/plugins/hub/protonmsg.py @@ -130,12 +130,20 @@ class TimeoutHandler(MessagingHandler): self.timeout_task.cancel() self.timeout_task = None + +def json_serialize(o): + """JSON helper to encode otherwise unserializable data types""" + if isinstance(o, set): + return list(o) + raise TypeError(repr(o) + " is not JSON serializable") + + def queue_msg(address, props, data): msgs = getattr(context, 'protonmsg_msgs', None) if msgs is None: msgs = [] context.protonmsg_msgs = msgs - body = json.dumps(data) + body = json.dumps(data, default=json_serialize) msgs.append((address, props, body)) @convert_datetime diff --git a/tests/test_plugins/test_protonmsg.py b/tests/test_plugins/test_protonmsg.py index 03a0484..b45d5e9 100644 --- a/tests/test_plugins/test_protonmsg.py +++ b/tests/test_plugins/test_protonmsg.py @@ -153,7 +153,8 @@ class TestProtonMsg(unittest.TestCase): user='test-user', **build) def test_prep_repo_init(self): - protonmsg.prep_repo_init('postRepoInit', tag={'name': 'test-tag'}, repo_id=1234) + protonmsg.prep_repo_init('postRepoInit', tag={'name': 'test-tag', + 'arches': set(['x86_64', 'i386'])}, repo_id=1234) self.assertMsg('repo.init', type='RepoInit', tag='test-tag', repo_id=1234) def test_prep_repo_done(self): From 21a8ee2ac1aac9a538272571c3f45c21ab796c22 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Mar 14 2018 09:54:27 +0000 Subject: [PATCH 2/2] Don't raise error on unserializable data --- diff --git a/plugins/hub/protonmsg.py b/plugins/hub/protonmsg.py index 134b42d..85165ba 100644 --- a/plugins/hub/protonmsg.py +++ b/plugins/hub/protonmsg.py @@ -135,7 +135,9 @@ def json_serialize(o): """JSON helper to encode otherwise unserializable data types""" if isinstance(o, set): return list(o) - raise TypeError(repr(o) + " is not JSON serializable") + log = logging.getLogger('koji.plugin.protonmsg') + log.error("Not JSON serializable data: %s" % repr(o)) + return {"error": "Can't serialize", "type": str(type(o))} def queue_msg(address, props, data):