-
-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from danballance/query_param_arrays
Fixed an issue with query parameters not properly handling arrays
- Loading branch information
Showing
4 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,22 @@ def get_function_arguments(function): # pragma: no cover | |
return inspect.getargspec(function).args | ||
|
||
|
||
def make_type(value, type): | ||
type_func = TYPE_MAP[type] # convert value to right type | ||
return type_func(value) | ||
|
||
|
||
def get_val_from_param(value, query_param): | ||
if query_param["type"] == "array": # then logic is more complex | ||
if query_param.get("collectionFormat") and query_param.get("collectionFormat") == "pipes": | ||
This comment has been minimized.
Sorry, something went wrong. |
||
parts = value.split("|") | ||
else: # default: csv | ||
parts = value.split(",") | ||
This comment has been minimized.
Sorry, something went wrong.
valgog
|
||
return [make_type(part, query_param["items"]["type"]) for part in parts] | ||
else: | ||
return make_type(value, query_param["type"]) | ||
|
||
|
||
def parameter_to_arg(parameters, function): | ||
""" | ||
Pass query and body parameters as keyword arguments to handler function. | ||
|
@@ -39,7 +55,7 @@ def parameter_to_arg(parameters, function): | |
""" | ||
body_parameters = [parameter for parameter in parameters if parameter['in'] == 'body'] or [{}] | ||
body_name = body_parameters[0].get('name') | ||
query_types = {parameter['name']: parameter['type'] | ||
query_types = {parameter['name']: parameter | ||
for parameter in parameters if parameter['in'] == 'query'} # type: dict[str, str] | ||
arguments = get_function_arguments(function) | ||
|
||
|
@@ -66,10 +82,9 @@ def wrapper(*args, **kwargs): | |
logger.debug("Query Parameter '%s' not in function arguments", key) | ||
else: | ||
logger.debug("Query Parameter '%s' in function arguments", key) | ||
key_type = query_types[key] | ||
logger.debug('%s is a %s', key, key_type) | ||
type_func = TYPE_MAP[key_type] # convert value to right type | ||
kwargs[key] = type_func(value) | ||
query_param = query_types[key] | ||
logger.debug('%s is a %s', key, query_param) | ||
kwargs[key] = get_val_from_param(value, query_param) | ||
|
||
return function(*args, **kwargs) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Actually it would be nice to check that other allowed values for the
collectionFormat
are not used, and throw an exception on the unsupported once. Current code will make it very difficult to understand whytsv
does not work.