summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Kotton <gkotton@vmware.com>2013-09-23 07:10:19 -0700
committerGary Kotton <gkotton@vmware.com>2013-09-23 07:10:19 -0700
commite1f45ba7b1002612317482336e666210c7aaf65e (patch)
treeadc49cd645e073c7cb90f9ffd34907a35fdc6269
parent127828a2f11f144cc2afe3499ac4a968f3826e00 (diff)
downloadoslo-serialization-e1f45ba7b1002612317482336e666210c7aaf65e.tar.gz
Ensure that Message objects will be sent via RPC in unicode format
Fixes bug 1229204 Change-Id: Ia915f3175bee370d747e362cd9d35d8536c1b7b2 Co-authored-by: He Jie Xu <xuhj@linux.vnet.ibm.com>
-rw-r--r--openstack/common/jsonutils.py3
-rw-r--r--tests/unit/test_jsonutils.py15
2 files changed, 18 insertions, 0 deletions
diff --git a/openstack/common/jsonutils.py b/openstack/common/jsonutils.py
index b64d69a..100a10e 100644
--- a/openstack/common/jsonutils.py
+++ b/openstack/common/jsonutils.py
@@ -46,6 +46,7 @@ except ImportError:
import six
+from openstack.common import gettextutils
from openstack.common import importutils
from openstack.common import timeutils
@@ -135,6 +136,8 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
if convert_datetime and isinstance(value, datetime.datetime):
return timeutils.strtime(value)
+ elif isinstance(value, gettextutils.Message):
+ return value.data
elif hasattr(value, 'iteritems'):
return recursive(dict(value.iteritems()), level=level + 1)
elif hasattr(value, '__iter__'):
diff --git a/tests/unit/test_jsonutils.py b/tests/unit/test_jsonutils.py
index 0bf811a..94d5076 100644
--- a/tests/unit/test_jsonutils.py
+++ b/tests/unit/test_jsonutils.py
@@ -25,6 +25,7 @@ import netaddr
import six
import testtools
+from openstack.common import gettextutils
from openstack.common import jsonutils
from openstack.common import test
@@ -181,3 +182,17 @@ class ToPrimitiveTestCase(test.BaseTestCase):
thing = {'ip_addr': netaddr.IPAddress('1.2.3.4')}
ret = jsonutils.to_primitive(thing)
self.assertEqual({'ip_addr': '1.2.3.4'}, ret)
+
+ def test_message_with_param(self):
+ message_with_params = 'A message with param: %s'
+ msg = gettextutils.Message(message_with_params, 'test_domain')
+ msg = msg % 'test_domain'
+ ret = jsonutils.to_primitive(msg)
+ self.assertEqual(msg, ret)
+
+ def test_message_with_named_param(self):
+ message_with_params = 'A message with params: %(param)s'
+ msg = gettextutils.Message(message_with_params, 'test_domain')
+ msg = msg % {'param': 'hello'}
+ ret = jsonutils.to_primitive(msg)
+ self.assertEqual(msg, ret)