From a94b9b4b25cd1e7d39ebef0b60f8c4367765777f Mon Sep 17 00:00:00 2001 From: Phil Day Date: Mon, 11 Feb 2013 10:52:35 +0000 Subject: to_primitive imposes what seems to be an arbitary data structure depth of 3, but there is at least on case in Nova (Security group Rules) which requires a depth beyond this. https://bugs.launchpad.net/nova/+bug/1118608 Specifically security_group_rule_get_by_security_group returns a set of rules which have the structure: rule -> grantee_group -> Instance -> Instance_type Rather than just bumping the depth limit, which might break some other user of to_primitive we make it a specific parameter that defaults to the current value but can be over-ridden when required and log a warning when the depth is exceeded Change-Id: I1eaebd484e20cb2eae09a693289709973de9943c --- openstack/common/jsonutils.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'openstack/common/jsonutils.py') 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 -- cgit