diff --git a/lower-constraints.txt b/lower-constraints.txt index ba0e7d8d8d09..292a9d5e65d7 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -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 diff --git a/nova/rpc.py b/nova/rpc.py index 6d7de3d67690..472698330533 100644 --- a/nova/rpc.py +++ b/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): diff --git a/nova/tests/fixtures.py b/nova/tests/fixtures.py index 6fc09b4cae5b..53e5a795124f 100644 --- a/nova/tests/fixtures.py +++ b/nova/tests/fixtures.py @@ -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 =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