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
This commit is contained in:
Thomas Herve 2016-06-09 15:53:45 +02:00
parent 9921b8ce6a
commit e97e3acf64
2 changed files with 42 additions and 2 deletions

View File

@ -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.

View File

@ -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