Merge "Fix jsonutils.to_primitive UserWarning"
This commit is contained in:
commit
05d3c01a68
|
@ -87,7 +87,7 @@ oslo.policy==1.35.0
|
|||
oslo.privsep==1.23.0
|
||||
oslo.reports==1.18.0
|
||||
oslo.rootwrap==5.8.0
|
||||
oslo.serialization==2.18.0
|
||||
oslo.serialization==2.21.1
|
||||
oslo.service==1.33.0
|
||||
oslo.upgradecheck==0.1.1
|
||||
oslo.utils==3.37.0
|
||||
|
|
23
nova/rpc.py
23
nova/rpc.py
|
@ -33,6 +33,7 @@ from oslo_messaging.rpc import dispatcher
|
|||
from oslo_serialization import jsonutils
|
||||
from oslo_service import periodic_task
|
||||
from oslo_utils import importutils
|
||||
import six
|
||||
|
||||
import nova.conf
|
||||
import nova.context
|
||||
|
@ -118,9 +119,27 @@ def get_allowed_exmods():
|
|||
|
||||
|
||||
class JsonPayloadSerializer(messaging.NoOpSerializer):
|
||||
|
||||
@staticmethod
|
||||
def serialize_entity(context, entity):
|
||||
return jsonutils.to_primitive(entity, convert_instances=True)
|
||||
def fallback(obj):
|
||||
"""Serializer fallback
|
||||
|
||||
This method is used to serialize an object which jsonutils.to_primitive
|
||||
does not otherwise know how to handle.
|
||||
|
||||
This is mostly only needed in tests because of the use of the nova
|
||||
CheatingSerializer fixture which keeps some non-serializable fields
|
||||
on the RequestContext, like db_connection.
|
||||
"""
|
||||
if isinstance(obj, nova.context.RequestContext):
|
||||
# This matches RequestContextSerializer.serialize_context().
|
||||
return obj.to_dict()
|
||||
# The default fallback in jsonutils.to_primitive() is six.text_type.
|
||||
return six.text_type(obj)
|
||||
|
||||
def serialize_entity(self, context, entity):
|
||||
return jsonutils.to_primitive(entity, convert_instances=True,
|
||||
fallback=self.fallback)
|
||||
|
||||
|
||||
class RequestContextSerializer(messaging.Serializer):
|
||||
|
|
|
@ -826,6 +826,15 @@ class WarningsFixture(fixtures.Fixture):
|
|||
# prevent adding violations.
|
||||
warnings.filterwarnings('error', message=".*invalid UUID.*")
|
||||
|
||||
# NOTE(mriedem): Avoid adding anything which tries to convert an
|
||||
# object to a primitive which jsonutils.to_primitive() does not know
|
||||
# how to handle (or isn't given a fallback callback).
|
||||
warnings.filterwarnings(
|
||||
'error',
|
||||
message="Cannot convert <oslo_db.sqlalchemy.enginefacade"
|
||||
"._Default object at ",
|
||||
category=UserWarning)
|
||||
|
||||
self.addCleanup(warnings.resetwarnings)
|
||||
|
||||
|
||||
|
|
|
@ -17,12 +17,16 @@ import functools
|
|||
import pprint
|
||||
import threading
|
||||
|
||||
from oslo_log import log as logging
|
||||
import oslo_messaging as messaging
|
||||
from oslo_serialization import jsonutils
|
||||
from oslo_utils import excutils
|
||||
from oslo_utils import timeutils
|
||||
|
||||
from nova import rpc
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class _Sub(object):
|
||||
"""Allow a subscriber to efficiently wait for an event to occur, and
|
||||
|
@ -96,7 +100,11 @@ class FakeNotifier(object):
|
|||
serializer=self._serializer)
|
||||
|
||||
def _notify(self, priority, ctxt, event_type, payload):
|
||||
payload = self._serializer.serialize_entity(ctxt, payload)
|
||||
try:
|
||||
payload = self._serializer.serialize_entity(ctxt, payload)
|
||||
except Exception:
|
||||
with excutils.save_and_reraise_exception():
|
||||
LOG.error('Error serializing payload: %s', payload)
|
||||
# NOTE(sileht): simulate the kombu serializer
|
||||
# this permit to raise an exception if something have not
|
||||
# been serialized correctly
|
||||
|
|
|
@ -18,6 +18,7 @@ import mock
|
|||
import oslo_messaging as messaging
|
||||
from oslo_messaging.rpc import dispatcher
|
||||
from oslo_serialization import jsonutils
|
||||
import six
|
||||
|
||||
from nova import context
|
||||
from nova import rpc
|
||||
|
@ -366,10 +367,20 @@ class TestRPC(test.NoDBTestCase):
|
|||
|
||||
class TestJsonPayloadSerializer(test.NoDBTestCase):
|
||||
def test_serialize_entity(self):
|
||||
serializer = rpc.JsonPayloadSerializer()
|
||||
with mock.patch.object(jsonutils, 'to_primitive') as mock_prim:
|
||||
rpc.JsonPayloadSerializer.serialize_entity('context', 'entity')
|
||||
serializer.serialize_entity('context', 'entity')
|
||||
|
||||
mock_prim.assert_called_once_with('entity', convert_instances=True)
|
||||
mock_prim.assert_called_once_with('entity', convert_instances=True,
|
||||
fallback=serializer.fallback)
|
||||
|
||||
def test_fallback(self):
|
||||
# Convert RequestContext, should get a dict.
|
||||
primitive = rpc.JsonPayloadSerializer.fallback(context.get_context())
|
||||
self.assertIsInstance(primitive, dict)
|
||||
# Convert anything else, should get a string.
|
||||
primitive = rpc.JsonPayloadSerializer.fallback(mock.sentinel.entity)
|
||||
self.assertIsInstance(primitive, six.text_type)
|
||||
|
||||
|
||||
class TestRequestContextSerializer(test.NoDBTestCase):
|
||||
|
|
|
@ -41,7 +41,7 @@ oslo.config>=6.1.0 # Apache-2.0
|
|||
oslo.context>=2.19.2 # Apache-2.0
|
||||
oslo.log>=3.36.0 # Apache-2.0
|
||||
oslo.reports>=1.18.0 # Apache-2.0
|
||||
oslo.serialization!=2.19.1,>=2.18.0 # Apache-2.0
|
||||
oslo.serialization!=2.19.1,>=2.21.1 # Apache-2.0
|
||||
oslo.upgradecheck>=0.1.1
|
||||
oslo.utils>=3.37.0 # Apache-2.0
|
||||
oslo.db>=4.40.0 # Apache-2.0
|
||||
|
|
Loading…
Reference in New Issue