Skip to content

Commit

Permalink
Merge pull request #172 from rafaelcaricio/master
Browse files Browse the repository at this point in the history
Type cast in path parameters
  • Loading branch information
jmcs committed Mar 7, 2016
2 parents 67a24b7 + 2558a61 commit dc29838
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 83 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ available type castings are:

+--------------+-------------+
| Swagger Type | Python Type |
| | |
+==============+=============+
| integer | int |
+--------------+-------------+
Expand Down
8 changes: 8 additions & 0 deletions connexion/decorators/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def parameter_to_arg(parameters, function):
for parameter in parameters if parameter['in'] == 'query'} # type: dict[str, str]
form_types = {parameter['name']: parameter
for parameter in parameters if parameter['in'] == 'formData'}
path_types = {parameter['name']: parameter
for parameter in parameters if parameter['in'] == 'path'}
arguments = get_function_arguments(function)
default_query_params = {param['name']: param['default']
for param in parameters if param['in'] == 'query' and 'default' in param}
Expand All @@ -83,6 +85,12 @@ def wrapper(*args, **kwargs):
if default_body and not request_body:
request_body = default_body

# Parse path parameters
for key, path_param_definitions in path_types.items():
if key in kwargs:
kwargs[key] = get_val_from_param(kwargs[key],
path_param_definitions)

# Add body parameters
if request_body is not None:
if body_name not in arguments:
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
# import sys
# import os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down
9 changes: 9 additions & 0 deletions tests/api/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,12 @@ def test_parameters_defined_in_path_level(simple_app):

resp = app_client.get('/v1.0/parameters-in-root-path')
assert resp.status_code == 400


def test_array_in_path(simple_app):
app_client = simple_app.app.test_client()
resp = app_client.get('/v1.0/test-array-in-path/one_item')
assert json.loads(resp.data.decode()) == ["one_item"]

resp = app_client.get('/v1.0/test-array-in-path/one_item,another_item')
assert json.loads(resp.data.decode()) == ["one_item", "another_item"]
2 changes: 1 addition & 1 deletion tests/fakeapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def get():
return ''
return ''
1 change: 1 addition & 0 deletions tests/fakeapi/foo_bar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3


def search():
return ''
4 changes: 4 additions & 0 deletions tests/fakeapi/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,7 @@ def path_parameters_in_get_method(title):

def test_default_missmatch_definition(age):
return 'OK'


def test_array_in_path(names):
return names, 200
22 changes: 22 additions & 0 deletions tests/fixtures/simple/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,28 @@ paths:
type: string
description: 204 no content

/test-array-in-path/{names}:
get:
operationId: fakeapi.hello.test_array_in_path
produces:
- application/json
parameters:
- name: names
description: List of names.
in: path
type: array
items:
type: string
required: true
collectionFormat: csv
responses:
200:
description: OK
schema:
type: array
items:
type: string


definitions:
new_stack:
Expand Down
158 changes: 79 additions & 79 deletions tests/test_operation.py

Large diffs are not rendered by default.

0 comments on commit dc29838

Please sign in to comment.