diff options
-rw-r--r-- | openstack/common/jsonutils.py | 13 | ||||
-rw-r--r-- | tests/unit/test_jsonutils.py | 26 |
2 files changed, 36 insertions, 3 deletions
diff --git a/openstack/common/jsonutils.py b/openstack/common/jsonutils.py index b7f6bcd..1ea7755 100644 --- a/openstack/common/jsonutils.py +++ b/openstack/common/jsonutils.py @@ -38,13 +38,17 @@ import functools import inspect import itertools import json +import logging import xmlrpclib +from openstack.common.gettextutils import _ from openstack.common import timeutils +LOG = logging.getLogger(__name__) + def to_primitive(value, convert_instances=False, convert_datetime=True, - level=0): + level=0, max_depth=3): """Convert a complex object into primitives. Handy for JSON serialization. We can optionally handle instances, @@ -80,7 +84,9 @@ def to_primitive(value, convert_instances=False, convert_datetime=True, if getattr(value, '__module__', None) == 'mox': return 'mock' - if level > 3: + if level > max_depth: + LOG.error(_('Max serialization depth exceeded on object: %d %s'), + level, value) return '?' # The try block may not be necessary after the class check above, @@ -89,7 +95,8 @@ def to_primitive(value, convert_instances=False, convert_datetime=True, recursive = functools.partial(to_primitive, convert_instances=convert_instances, convert_datetime=convert_datetime, - level=level) + level=level, + max_depth=max_depth) # It's not clear why xmlrpclib created their own DateTime type, but # for our purposes, make it a datetime type which is explicitly # handled diff --git a/tests/unit/test_jsonutils.py b/tests/unit/test_jsonutils.py index 87ac367..276ae97 100644 --- a/tests/unit/test_jsonutils.py +++ b/tests/unit/test_jsonutils.py @@ -143,3 +143,29 @@ class ToPrimitiveTestCase(utils.BaseTestCase): self.assertTrue(ret[0].startswith(u"<module 'datetime' from ")) self.assertTrue(ret[1].startswith('<function foo at 0x')) self.assertEquals(ret[2], '<built-in function dir>') + + def test_depth(self): + class LevelsGenerator(object): + def __init__(self, levels): + self._levels = levels + + def iteritems(self): + if self._levels == 0: + return iter([]) + else: + return iter([(0, LevelsGenerator(self._levels - 1))]) + + l4_obj = LevelsGenerator(4) + + json_l2 = {0: {0: '?'}} + json_l3 = {0: {0: {0: '?'}}} + json_l4 = {0: {0: {0: {0: '?'}}}} + + ret = jsonutils.to_primitive(l4_obj, max_depth=2) + self.assertEquals(ret, json_l2) + + ret = jsonutils.to_primitive(l4_obj, max_depth=3) + self.assertEquals(ret, json_l3) + + ret = jsonutils.to_primitive(l4_obj, max_depth=4) + self.assertEquals(ret, json_l4) |