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