Handle [] {} for body in rest protocols
This change allow to use [] or {} for the definition of the body in rest protocols. Fixes bug #1233219 Change-Id: Ib96f0487dd7d78bd657f6d4b3facbd8b611f8702
This commit is contained in:
parent
259fea9529
commit
664c214dbf
@ -105,6 +105,7 @@ class FunctionDefinition(object):
|
||||
|
||||
def resolve_types(self, registry):
|
||||
self.return_type = registry.resolve_type(self.return_type)
|
||||
self.body_type = registry.resolve_type(self.body_type)
|
||||
for arg in self.arguments:
|
||||
arg.resolve_type(registry)
|
||||
|
||||
|
@ -308,6 +308,30 @@ class ArgTypes(object):
|
||||
return value
|
||||
|
||||
|
||||
class BodyTypes(object):
|
||||
def assertEquals(self, a, b):
|
||||
if not (a == b):
|
||||
raise AssertionError('%s != %s' % (a, b))
|
||||
|
||||
@expose(int, body={wsme.types.text: int})
|
||||
@validate(int)
|
||||
def setdict(self, body):
|
||||
print(body)
|
||||
self.assertEquals(type(body), dict)
|
||||
self.assertEquals(type(body['test']), int)
|
||||
self.assertEquals(body['test'], 10)
|
||||
return body['test']
|
||||
|
||||
@expose(int, body=[int])
|
||||
@validate(int)
|
||||
def setlist(self, body):
|
||||
print(body)
|
||||
self.assertEquals(type(body), list)
|
||||
self.assertEquals(type(body[0]), int)
|
||||
self.assertEquals(body[0], 10)
|
||||
return body[0]
|
||||
|
||||
|
||||
class WithErrors(object):
|
||||
@expose()
|
||||
def divide_by_zero(self):
|
||||
@ -324,6 +348,7 @@ class MiscFunctions(object):
|
||||
class WSTestRoot(WSRoot):
|
||||
argtypes = ArgTypes()
|
||||
returntypes = ReturnTypes()
|
||||
bodytypes = BodyTypes()
|
||||
witherrors = WithErrors()
|
||||
nested = NestedOuterApi()
|
||||
misc = MiscFunctions()
|
||||
@ -653,3 +678,15 @@ class ProtocolTestCase(unittest.TestCase):
|
||||
res = self.call('argtypes/setdatetime', _accept="text/html",
|
||||
_no_result_decode=True)
|
||||
self.assertEquals(res.content_type, 'text/html')
|
||||
|
||||
|
||||
class RestOnlyProtocolTestCase(ProtocolTestCase):
|
||||
def test_body_list(self):
|
||||
r = self.call('bodytypes/setlist', body=([10], [int]), _rt=int)
|
||||
self.assertEqual(r, 10)
|
||||
|
||||
def test_body_dict(self):
|
||||
r = self.call('bodytypes/setdict',
|
||||
body=({'test': 10}, {wsme.types.text: int}),
|
||||
_rt=int)
|
||||
self.assertEqual(r, 10)
|
||||
|
@ -140,19 +140,27 @@ class MiniCrud(object):
|
||||
wsme.tests.protocol.WSTestRoot.crud = MiniCrud()
|
||||
|
||||
|
||||
class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
||||
class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase):
|
||||
protocol = 'restjson'
|
||||
|
||||
def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False,
|
||||
**kw):
|
||||
for key in kw:
|
||||
if isinstance(kw[key], tuple):
|
||||
value, datatype = kw[key]
|
||||
body=None, **kw):
|
||||
if body:
|
||||
if isinstance(body, tuple):
|
||||
body, datatype = body
|
||||
else:
|
||||
value = kw[key]
|
||||
datatype = type(value)
|
||||
kw[key] = prepare_value(value, datatype)
|
||||
content = json.dumps(kw)
|
||||
datatype = type(body)
|
||||
body = prepare_value(body, datatype)
|
||||
content = json.dumps(body)
|
||||
else:
|
||||
for key in kw:
|
||||
if isinstance(kw[key], tuple):
|
||||
value, datatype = kw[key]
|
||||
else:
|
||||
value = kw[key]
|
||||
datatype = type(value)
|
||||
kw[key] = prepare_value(value, datatype)
|
||||
content = json.dumps(kw)
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
@ -117,12 +117,15 @@ def loadxml(el, datatype):
|
||||
return datatype(el.text)
|
||||
|
||||
|
||||
class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
|
||||
class TestRestXML(wsme.tests.protocol.RestOnlyProtocolTestCase):
|
||||
protocol = 'restxml'
|
||||
|
||||
def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False,
|
||||
**kw):
|
||||
el = dumpxml('parameters', kw)
|
||||
body=None, **kw):
|
||||
if body:
|
||||
el = dumpxml('body', body)
|
||||
else:
|
||||
el = dumpxml('parameters', kw)
|
||||
content = et.tostring(el)
|
||||
headers = {
|
||||
'Content-Type': 'text/xml',
|
||||
|
@ -18,7 +18,7 @@ class TestSpore(unittest.TestCase):
|
||||
|
||||
spore = json.loads(spore)
|
||||
|
||||
assert len(spore['methods']) == 47, str(len(spore['methods']))
|
||||
assert len(spore['methods']) == 49, str(len(spore['methods']))
|
||||
|
||||
m = spore['methods']['argtypes_setbytesarray']
|
||||
assert m['path'] == 'argtypes/setbytesarray', m['path']
|
||||
|
@ -402,7 +402,7 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase):
|
||||
|
||||
assert len(sd.ports) == 1
|
||||
port, methods = sd.ports[0]
|
||||
self.assertEquals(len(methods), 47)
|
||||
self.assertEquals(len(methods), 49)
|
||||
|
||||
methods = dict(methods)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user