Skip to content

Commit

Permalink
Preserving the multiplicity of array type query parameters (#500)
Browse files Browse the repository at this point in the history
* Preserving the multiplicity of array type query parameters

* Resolved failing tests, and refactored code block into a method

* Added tests for multi array

* Refactored parameter.py and updated tests in test_parameters.py
  • Loading branch information
lhannest authored and hjacobs committed Sep 8, 2017
1 parent 08dd554 commit f36c246
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 18 additions & 1 deletion connexion/decorators/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ def sanitize_param(name):
name = snake_and_shadow(name)
return name and re.sub('^[^a-zA-Z_]+', '', re.sub('[^0-9a-zA-Z_]', '', name))

def make_request_query(request):
request_query = {}
try:
for k, v in request.query.to_dict(flat=False).items():
k = sanitize_param(k)
query_param = query_types.get(k, None)
if query_param is not None and query_param["type"] == "array":
if query_param.get("collectionFormat", None) == "pipes":
request_query[k] = "|".join(v)
else:
request_query[k] = ",".join(v)
else:
request_query[k] = v[0]
except AttributeError:
request_query = {sanitize_param(k): v for k, v in request.form.items()}
return request_query

body_parameters = [parameter for parameter in parameters if parameter['in'] == 'body'] or [{}]
body_name = sanitize_param(body_parameters[0].get('name'))
default_body = body_parameters[0].get('schema', {}).get('default')
Expand Down Expand Up @@ -150,7 +167,7 @@ def wrapper(request):

# Add query parameters
query_arguments = copy.deepcopy(default_query_params)
query_arguments.update({sanitize_param(k): v for k, v in request.query.items()})
query_arguments.update(make_request_query(request))
for key, value in query_arguments.items():
if not has_kwargs and key not in arguments:
logger.debug("Query Parameter '%s' not in function arguments", key)
Expand Down
8 changes: 8 additions & 0 deletions tests/api/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def test_array_query_param(simple_app):
response = app_client.get(url, headers=headers)
array_response = json.loads(response.data.decode('utf-8', 'replace')) # [str] unsupported collectionFormat
assert array_response == ["1;2;3"]
url = '/v1.0/test_array_csv_query_param?items=A&items=B&items=C&items=D,E,F'
response = app_client.get(url, headers=headers)
array_response = json.loads(response.data.decode('utf-8', 'replace')) # type: [str] multi array with csv format
assert array_response == ['A', 'B', 'C', 'D', 'E', 'F']
url = '/v1.0/test_array_pipes_query_param?items=4&items=5&items=6&items=7|8|9'
response = app_client.get(url, headers=headers)
array_response = json.loads(response.data.decode('utf-8', 'replace')) # type: [int] multi array with pipes format
assert array_response == [4, 5, 6, 7, 8, 9]


def test_extra_query_param(simple_app):
Expand Down

0 comments on commit f36c246

Please sign in to comment.