From f7098011a28031aa54b9f358650a8cd2e3235b40 Mon Sep 17 00:00:00 2001 From: Hiroaki Kobayashi Date: Fri, 12 Jan 2018 14:56:20 +0900 Subject: [PATCH] Fix JSON serialization issues with Python3 Partially implements: blueprint python-3 Change-Id: I0f4ec3470529dfcda6fa9056d0785ec93c50d5dd --- blazar/api/app.py | 6 +++--- blazar/api/context.py | 5 ++--- blazar/api/root.py | 4 ++-- blazar/api/v1/utils.py | 2 +- blazar/api/v2/controllers/types.py | 4 ++-- blazar/api/v2/middleware/__init__.py | 7 +++---- blazar/db/sqlalchemy/types.py | 2 +- blazar/tests/api/test_context.py | 4 ++-- blazar/tests/api/test_root.py | 4 ++-- blazar/tests/api/test_version_selector.py | 12 ++++++------ blazar/utils/plugins.py | 5 ++--- 11 files changed, 26 insertions(+), 29 deletions(-) diff --git a/blazar/api/app.py b/blazar/api/app.py index ae172615..77ddb095 100644 --- a/blazar/api/app.py +++ b/blazar/api/app.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils from blazar.api.v1 import app as v1_app from blazar.api.v2 import app as v2_app @@ -31,7 +31,7 @@ class VersionSelectorApplication(object): def _append_versions_from_app(self, versions, app, environ): tmp_versions = app(environ, self.internal_start_response) if self._status.startswith("300"): - tmp_versions = json.loads("".join(tmp_versions)) + tmp_versions = jsonutils.loads(tmp_versions.pop()) versions['versions'].extend(tmp_versions['versions']) def internal_start_response(self, status, response_headers, exc_info=None): @@ -49,7 +49,7 @@ class VersionSelectorApplication(object): if len(versions['versions']): start_response("300 Multiple Choices", [("Content-Type", "application/json")]) - return [json.dumps(versions)] + return [jsonutils.dump_as_bytes(versions)] else: start_response("204 No Content", []) return [] diff --git a/blazar/api/context.py b/blazar/api/context.py index 84571801..da1ecf61 100644 --- a/blazar/api/context.py +++ b/blazar/api/context.py @@ -13,8 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - +from oslo_serialization import jsonutils import six from blazar import context @@ -23,7 +22,7 @@ from blazar import exceptions def ctx_from_headers(headers): try: - service_catalog = json.loads(headers['X-Service-Catalog']) + service_catalog = jsonutils.loads(headers['X-Service-Catalog']) except KeyError: raise exceptions.ServiceCatalogNotFound() except TypeError: diff --git a/blazar/api/root.py b/blazar/api/root.py index 74d2355e..2799b7be 100644 --- a/blazar/api/root.py +++ b/blazar/api/root.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils import pecan from blazar.api.v2 import controllers @@ -37,7 +37,7 @@ class RootController(object): versions = {"versions": []} self._append_versions_from_controller(versions['versions'], self.v2, 'v2') - return json.dumps(versions) + return jsonutils.dump_as_bytes(versions) @pecan.expose(content_type='application/json') def versions(self): diff --git a/blazar/api/v1/utils.py b/blazar/api/v1/utils.py index f03bf2df..0641a883 100644 --- a/blazar/api/v1/utils.py +++ b/blazar/api/v1/utils.py @@ -160,7 +160,7 @@ def render(result=None, response_type=None, status=None, **kwargs): _("Content type '%s' isn't supported") % response_type) return - body = serializer.dumps(result) + body = serializer.dump_as_bytes(result) response_type = str(response_type) diff --git a/blazar/api/v2/controllers/types.py b/blazar/api/v2/controllers/types.py index d1cf1baf..a799cebb 100644 --- a/blazar/api/v2/controllers/types.py +++ b/blazar/api/v2/controllers/types.py @@ -14,9 +14,9 @@ # limitations under the License. import datetime -import json import uuid +from oslo_serialization import jsonutils import six from wsme import types as wtypes from wsme import utils as wutils @@ -81,7 +81,7 @@ class CPUInfo(wtypes.UserType): # another. We need to keep this method as generic as # possible, ie. we accept JSONified dict. try: - cpu_info = json.loads(value) + cpu_info = jsonutils.loads(value) except TypeError: raise exceptions.InvalidInput(cls=CPUInfo.name, value=value) if not isinstance(cpu_info, dict): diff --git a/blazar/api/v2/middleware/__init__.py b/blazar/api/v2/middleware/__init__.py index c8edb109..fa22f4a6 100644 --- a/blazar/api/v2/middleware/__init__.py +++ b/blazar/api/v2/middleware/__init__.py @@ -13,9 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_log import log as logging +from oslo_serialization import jsonutils import webob from blazar.db import exceptions as db_exceptions @@ -79,7 +78,7 @@ class ParsableErrorMiddleware(object): if not state: return app_iter try: - res_dct = json.loads(app_iter[0]) + res_dct = jsonutils.loads(app_iter[0]) except ValueError: return app_iter else: @@ -128,7 +127,7 @@ class ParsableErrorMiddleware(object): state['status_code'] = cls.code # NOTE(sbauza): Client expects a JSON encoded dict - body = [json.dumps( + body = [jsonutils.dump_as_bytes( {'error_code': state['status_code'], 'error_message': faultstring, 'error_name': state['status_code']} diff --git a/blazar/db/sqlalchemy/types.py b/blazar/db/sqlalchemy/types.py index bf7afe46..65a11e9c 100644 --- a/blazar/db/sqlalchemy/types.py +++ b/blazar/db/sqlalchemy/types.py @@ -24,7 +24,7 @@ class JsonEncoded(sa.TypeDecorator): def process_bind_param(self, value, dialect): if value is not None: - value = jsonutils.dumps(value) + value = jsonutils.dump_as_bytes(value) return value def process_result_value(self, value, dialect): diff --git a/blazar/tests/api/test_context.py b/blazar/tests/api/test_context.py index fa8102e1..96e089bd 100644 --- a/blazar/tests/api/test_context.py +++ b/blazar/tests/api/test_context.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils from blazar.api import context as api_context from blazar import context @@ -34,7 +34,7 @@ class ContextTestCase(tests.TestCase): def test_ctx_from_headers(self): self.context = self.patch(context, 'BlazarContext') - catalog = json.dumps({'nova': 'catalog'}) + catalog = jsonutils.dump_as_bytes({'nova': 'catalog'}) self.fake_headers[u'X-Service-Catalog'] = catalog api_context.ctx_from_headers(self.fake_headers) self.context.assert_called_once_with(user_id=u'1', diff --git a/blazar/tests/api/test_root.py b/blazar/tests/api/test_root.py index 6f0e0378..f3fd42f4 100644 --- a/blazar/tests/api/test_root.py +++ b/blazar/tests/api/test_root.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils from blazar.tests import api @@ -21,7 +21,7 @@ from blazar.tests import api class TestRoot(api.APITest): def setUp(self): super(TestRoot, self).setUp() - self.versions = json.dumps( + self.versions = jsonutils.dump_as_bytes( {"versions": [{"status": "CURRENT", "id": "v2.0", diff --git a/blazar/tests/api/test_version_selector.py b/blazar/tests/api/test_version_selector.py index 10361e54..79ccf21a 100644 --- a/blazar/tests/api/test_version_selector.py +++ b/blazar/tests/api/test_version_selector.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json +from oslo_serialization import jsonutils from blazar.api import app as api from blazar.api.v1 import app as v1_app @@ -35,7 +35,7 @@ class FakeWSGIApp(object): def __call__(self, environ, start_response): start_response(self.status_code, []) - return [json.dumps(self.versions)] + return [jsonutils.dump_as_bytes(self.versions)] class TestVersionDiscovery(tests.TestCase): @@ -62,7 +62,7 @@ class TestVersionDiscovery(tests.TestCase): environ = {'PATH_INFO': self.path} versions_raw = version_selector(environ, self.start_response) - versions = json.loads("".join(versions_raw)) + versions = jsonutils.loads(versions_raw.pop()) self.assertEqual(2, len(versions['versions'])) self.assertEqual("v{0}".format(self.v1_make_app().id_version), @@ -79,7 +79,7 @@ class TestVersionDiscovery(tests.TestCase): environ = {'PATH_INFO': self.path} versions_raw = version_selector(environ, self.start_response) - versions = json.loads("".join(versions_raw)) + versions = jsonutils.loads(versions_raw.pop()) self.assertEqual(1, len(versions['versions'])) self.assertEqual("v{0}".format(self.v1_make_app().id_version), @@ -117,7 +117,7 @@ class TestVersionSelectorApplication(tests.TestCase): environ = {'PATH_INFO': "/v1"} versions_raw = version_selector(environ, self.start_response) - versions = json.loads("".join(versions_raw)) + versions = jsonutils.loads(versions_raw.pop()) self.assertEqual(self.v1_make_app().versions, versions) def test_get_v2_app(self): @@ -125,5 +125,5 @@ class TestVersionSelectorApplication(tests.TestCase): environ = {'PATH_INFO': "/v2"} versions_raw = version_selector(environ, self.start_response) - versions = json.loads("".join(versions_raw)) + versions = jsonutils.loads(versions_raw.pop()) self.assertEqual(self.v2_make_app().versions, versions) diff --git a/blazar/utils/plugins.py b/blazar/utils/plugins.py index a39dab7d..c437f537 100644 --- a/blazar/utils/plugins.py +++ b/blazar/utils/plugins.py @@ -13,8 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - +from oslo_serialization import jsonutils import six from blazar.manager import exceptions as manager_ex @@ -30,7 +29,7 @@ def convert_requirements(requirements): # Convert text to json if isinstance(requirements, six.string_types): try: - requirements = json.loads(requirements) + requirements = jsonutils.loads(requirements) except ValueError: raise manager_ex.MalformedRequirements(rqrms=requirements)