Fix array as input GET parameters in the pecan adapter
This commit is contained in:
parent
2758202d77
commit
9aa7055f55
@ -37,12 +37,28 @@ class BooksController(RestController):
|
|||||||
return book
|
return book
|
||||||
|
|
||||||
|
|
||||||
|
class Criterion(Base):
|
||||||
|
op = text
|
||||||
|
attrname = text
|
||||||
|
value = text
|
||||||
|
|
||||||
|
|
||||||
class AuthorsController(RestController):
|
class AuthorsController(RestController):
|
||||||
|
|
||||||
books = BooksController()
|
books = BooksController()
|
||||||
|
|
||||||
@wsme.pecan.wsexpose([Author])
|
@wsme.pecan.wsexpose([Author], [unicode], [Criterion])
|
||||||
def get_all(self):
|
def get_all(self, q=None, r=None):
|
||||||
|
if q:
|
||||||
|
return [
|
||||||
|
Author(id=i, firstname=value)
|
||||||
|
for i, value in enumerate(q)
|
||||||
|
]
|
||||||
|
if r:
|
||||||
|
return [
|
||||||
|
Author(id=i, firstname=c.value)
|
||||||
|
for i, c in enumerate(r)
|
||||||
|
]
|
||||||
return [
|
return [
|
||||||
Author(id=1, firstname=u'FirstName')
|
Author(id=1, firstname=u'FirstName')
|
||||||
]
|
]
|
||||||
|
@ -7,6 +7,38 @@ class TestWS(FunctionalTest):
|
|||||||
def test_get_all(self):
|
def test_get_all(self):
|
||||||
self.app.get('/authors')
|
self.app.get('/authors')
|
||||||
|
|
||||||
|
def test_optional_array_param(self):
|
||||||
|
r = self.app.get('/authors?q=a&q=b')
|
||||||
|
l = json.loads(r.body)
|
||||||
|
print l
|
||||||
|
assert len(l) == 2
|
||||||
|
assert l[0]['firstname'] == 'a'
|
||||||
|
assert l[1]['firstname'] == 'b'
|
||||||
|
|
||||||
|
def test_optional_indexed_array_param(self):
|
||||||
|
r = self.app.get('/authors?q[0]=a&q[1]=b')
|
||||||
|
l = json.loads(r.body)
|
||||||
|
print l
|
||||||
|
assert len(l) == 2
|
||||||
|
assert l[0]['firstname'] == 'a'
|
||||||
|
assert l[1]['firstname'] == 'b'
|
||||||
|
|
||||||
|
def test_options_object_array_param(self):
|
||||||
|
r = self.app.get('/authors?r.value=a&r.value=b')
|
||||||
|
l = json.loads(r.body)
|
||||||
|
print l
|
||||||
|
assert len(l) == 2
|
||||||
|
assert l[0]['firstname'] == 'a'
|
||||||
|
assert l[1]['firstname'] == 'b'
|
||||||
|
|
||||||
|
def test_options_indexed_object_array_param(self):
|
||||||
|
r = self.app.get('/authors?r[0].value=a&r[1].value=b')
|
||||||
|
l = json.loads(r.body)
|
||||||
|
print l
|
||||||
|
assert len(l) == 2
|
||||||
|
assert l[0]['firstname'] == 'a'
|
||||||
|
assert l[1]['firstname'] == 'b'
|
||||||
|
|
||||||
def test_get_author(self):
|
def test_get_author(self):
|
||||||
a = self.app.get(
|
a = self.app.get(
|
||||||
'/authors/1.json',
|
'/authors/1.json',
|
||||||
|
@ -62,7 +62,7 @@ def wsexpose(*args, **kwargs):
|
|||||||
def callfunction(self, *args, **kwargs):
|
def callfunction(self, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
args, kwargs = wsme.rest.args.get_args(
|
args, kwargs = wsme.rest.args.get_args(
|
||||||
funcdef, args, kwargs,
|
funcdef, args, kwargs, pecan.request.params,
|
||||||
pecan.request.body, pecan.request.content_type
|
pecan.request.body, pecan.request.content_type
|
||||||
)
|
)
|
||||||
result = f(self, *args, **kwargs)
|
result = f(self, *args, **kwargs)
|
||||||
|
@ -46,6 +46,16 @@ def usertype_from_param(datatype, value):
|
|||||||
from_param(datatype.basetype, value))
|
from_param(datatype.basetype, value))
|
||||||
|
|
||||||
|
|
||||||
|
@from_param.when_type(ArrayType)
|
||||||
|
def array_from_param(datatype, value):
|
||||||
|
if value is None:
|
||||||
|
return value
|
||||||
|
return [
|
||||||
|
from_param(datatype.item_type, item)
|
||||||
|
for item in value
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@generic
|
@generic
|
||||||
def from_params(datatype, params, path, hit_paths):
|
def from_params(datatype, params, path, hit_paths):
|
||||||
if iscomplex(datatype) and datatype is not File:
|
if iscomplex(datatype) and datatype is not File:
|
||||||
@ -72,6 +82,7 @@ def from_params(datatype, params, path, hit_paths):
|
|||||||
@from_params.when_type(ArrayType)
|
@from_params.when_type(ArrayType)
|
||||||
def array_from_params(datatype, params, path, hit_paths):
|
def array_from_params(datatype, params, path, hit_paths):
|
||||||
if path in params:
|
if path in params:
|
||||||
|
hit_paths.add(path)
|
||||||
return [
|
return [
|
||||||
from_param(datatype.item_type, value)
|
from_param(datatype.item_type, value)
|
||||||
for value in params.getall(path)]
|
for value in params.getall(path)]
|
||||||
@ -175,9 +186,10 @@ def combine_args(funcdef, *akw):
|
|||||||
return newargs, newkwargs
|
return newargs, newkwargs
|
||||||
|
|
||||||
|
|
||||||
def get_args(funcdef, args, kwargs, body, mimetype):
|
def get_args(funcdef, args, kwargs, params, body, mimetype):
|
||||||
return combine_args(
|
return combine_args(
|
||||||
funcdef,
|
funcdef,
|
||||||
args_from_args(funcdef, args, kwargs),
|
args_from_args(funcdef, args, kwargs),
|
||||||
args_from_body(funcdef, body, mimetype)
|
args_from_params(funcdef, params),
|
||||||
|
args_from_body(funcdef, body, mimetype),
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user