From e97e3acf64220677b9caa507b2dd13338e6fe3e9 Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Thu, 9 Jun 2016 15:53:45 +0200 Subject: [PATCH] Support JSON data in JSON API type JSON API type now tries to load a string all the time, even if the data is already decoded as part of the request. It seems easy to support loading the data directly. We also handle TypeError so that we handle non-dict incorrect values. Change-Id: Ib8c21d95df96743e65bdf2bc88af5207cf78fd8f Closes-Bug: #1590789 --- mistral/api/controllers/v2/types.py | 8 +++-- .../unit/api/v2/test_action_executions.py | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/mistral/api/controllers/v2/types.py b/mistral/api/controllers/v2/types.py index 2f0efac5..ca474c2d 100644 --- a/mistral/api/controllers/v2/types.py +++ b/mistral/api/controllers/v2/types.py @@ -107,8 +107,12 @@ class JsonType(wtypes.UserType): return value def frombasetype(self, value): - # Value must be a string. - return json.loads(value) if value is not None else None + if isinstance(value, dict): + return value + try: + return json.loads(value) if value is not None else None + except TypeError as e: + raise ValueError(e) def tobasetype(self, value): # Value must be a dict. diff --git a/mistral/tests/unit/api/v2/test_action_executions.py b/mistral/tests/unit/api/v2/test_action_executions.py index 11aa0cb6..259f2771 100644 --- a/mistral/tests/unit/api/v2/test_action_executions.py +++ b/mistral/tests/unit/api/v2/test_action_executions.py @@ -197,6 +197,33 @@ class TestActionExecutionsController(base.APITest): save_result=True ) + @mock.patch.object(rpc.EngineClient, 'start_action') + def test_post_json(self, f): + f.return_value = ACTION_EX_DB.to_dict() + + resp = self.app.post_json( + '/v2/action_executions', + { + 'name': 'std.echo', + 'input': {}, + 'params': '{"save_result": true}' + } + ) + + self.assertEqual(201, resp.status_int) + + action_exec = ACTION_EX + del action_exec['task_name'] + + self.assertDictEqual(ACTION_EX, resp.json) + + f.assert_called_once_with( + ACTION_EX['name'], + json.loads(ACTION_EX['input']), + description=None, + save_result=True + ) + @mock.patch.object(rpc.EngineClient, 'start_action') def test_post_without_input(self, f): f.return_value = ACTION_EX_DB.to_dict() @@ -230,6 +257,15 @@ class TestActionExecutionsController(base.APITest): self.assertEqual(400, resp.status_int) + def test_post_bad_json_input(self): + resp = self.app.post_json( + '/v2/action_executions', + {'input': 2}, + expect_errors=True + ) + + self.assertEqual(400, resp.status_int) + @mock.patch.object(rpc.EngineClient, 'on_action_complete') def test_put(self, f): f.return_value = UPDATED_ACTION_EX_DB