From 5aa8cf367a9adaf2f9a03033c4ed5a48a037860c Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Mon, 28 Nov 2011 18:51:31 +0100 Subject: [PATCH] The restjson return values are not nested in the result attribute of an object. The former behavior can be obtained with the nest_result protocol option --- wsme/protocols/restjson.py | 8 +++++++- wsme/tests/test_restjson.py | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/wsme/protocols/restjson.py b/wsme/protocols/restjson.py index 912c676..9a59143 100644 --- a/wsme/protocols/restjson.py +++ b/wsme/protocols/restjson.py @@ -170,6 +170,10 @@ class RestJsonProtocol(RestProtocol): 'text/javascript', ''] + def __init__(self, nest_result=False): + super(RestJsonProtocol, self).__init__() + self.nest_result = nest_result + def decode_arg(self, value, arg): return fromjson(arg.datatype, value) @@ -182,7 +186,9 @@ class RestJsonProtocol(RestProtocol): def encode_result(self, context, result): r = tojson(context.funcdef.return_type, result) - return json.dumps({'result': r}, ensure_ascii=False).encode('utf8') + if self.nest_result: + r = {'result': r} + return json.dumps(r, ensure_ascii=False).encode('utf8') def encode_error(self, context, errordetail): return json.dumps(errordetail, encoding='utf-8') diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py index f7052bc..a853068 100644 --- a/wsme/tests/test_restjson.py +++ b/wsme/tests/test_restjson.py @@ -1,5 +1,6 @@ import decimal import base64 +import datetime import wsme.tests.protocol @@ -10,7 +11,7 @@ except: import wsme.protocols.restjson from wsme.protocols.restjson import fromjson -from wsme.utils import * +from wsme.utils import parse_isodatetime, parse_isotime, parse_isodate from wsme.types import isusertype @@ -79,8 +80,7 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase): return res r = json.loads(res.body) - if 'result' in r: - r = r['result'] + if res.status_int == 200: if _rt and r: r = prepare_result(r, _rt) return r @@ -97,4 +97,12 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase): def test_keyargs(self): r = self.app.get('/argtypes/setint.json?value=2') - assert json.loads(r.body) == {'result': 2} + print r + assert json.loads(r.body) == 2 + + nestedarray = 'value[0].inner.aint=54&value[1].inner.aint=55' + r = self.app.get('/argtypes/setnestedarray.json?' + nestedarray) + print r + assert json.loads(r.body) == [ + {'inner': {'aint': 54}}, + {'inner': {'aint': 55}}]