Skip to content

Commit

Permalink
#104 support type casting of int/float path parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Dec 5, 2015
1 parent 0c56d15 commit c7a1105
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions connexion/api.py
Expand Up @@ -118,7 +118,8 @@ def add_operation(self, method, path, swagger_operation):
operation_id = operation.operation_id
logger.debug('... Adding %s -> %s', method.upper(), operation_id, extra=vars(operation))

self.blueprint.add_url_rule(path, operation.endpoint_name, operation.function, methods=[method])
flask_path = utils.flaskify_path(path, operation.get_path_parameter_types())
self.blueprint.add_url_rule(flask_path, operation.endpoint_name, operation.function, methods=[method])

def add_paths(self, paths=None):
"""
Expand All @@ -129,7 +130,6 @@ def add_paths(self, paths=None):
paths = paths or self.specification.get('paths', dict())
for path, methods in paths.items():
logger.debug('Adding %s%s...', self.base_url, path)
path = utils.flaskify_path(path)
# TODO Error handling
for method, endpoint in methods.items():
try:
Expand Down
3 changes: 3 additions & 0 deletions connexion/operation.py
Expand Up @@ -134,6 +134,9 @@ def resolve_parameters(self, parameters):
param = self.resolve_reference(param)
yield param

def get_path_parameter_types(self):
return {p['name']: p.get('type') for p in self.parameters if p['in'] == 'path'}

@property
def body_schema(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/fakeapi/api.yaml
Expand Up @@ -563,6 +563,14 @@ paths:
- name: someint
in: path
type: integer
/test-float-path/{somefloat}:
get:
summary: Test type casting of path parameter
operationId: fakeapi.hello.test_get_somefloat
parameters:
- name: somefloat
in: path
type: number
definitions:
new_stack:
type: object
Expand Down
6 changes: 5 additions & 1 deletion tests/fakeapi/hello.py
Expand Up @@ -186,4 +186,8 @@ def test_schema_int(test_int):


def test_get_someint(someint):
return str(type(someint))
return type(someint).__name__


def test_get_somefloat(somefloat):
return type(somefloat).__name__
8 changes: 7 additions & 1 deletion tests/test_app.py
Expand Up @@ -528,4 +528,10 @@ def test_resolve_classmethod(app):
def test_path_parameter_someint(app):
app_client = app.app.test_client()
resp = app_client.get('/v1.0/test-int-path/123') # type: flask.Response
assert resp.data.decode() == '"<type \'int\'>"'
assert resp.data.decode() == '"int"'


def test_path_parameter_somefloat(app):
app_client = app.app.test_client()
resp = app_client.get('/v1.0/test-float-path/123.45') # type: flask.Response
assert resp.data.decode() == '"float"'

0 comments on commit c7a1105

Please sign in to comment.