From c7a1105cc0cf8fffa147838605a9695878e0f9e9 Mon Sep 17 00:00:00 2001 From: Henning Jacobs Date: Sat, 5 Dec 2015 20:55:12 +0100 Subject: [PATCH] #104 support type casting of int/float path parameters --- connexion/api.py | 4 ++-- connexion/operation.py | 3 +++ tests/fakeapi/api.yaml | 8 ++++++++ tests/fakeapi/hello.py | 6 +++++- tests/test_app.py | 8 +++++++- 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/connexion/api.py b/connexion/api.py index c7f488ad0..fb8062c39 100644 --- a/connexion/api.py +++ b/connexion/api.py @@ -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): """ @@ -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: diff --git a/connexion/operation.py b/connexion/operation.py index 7a04852bf..0dc440b51 100644 --- a/connexion/operation.py +++ b/connexion/operation.py @@ -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): """ diff --git a/tests/fakeapi/api.yaml b/tests/fakeapi/api.yaml index b2bf62f70..709d8c1db 100644 --- a/tests/fakeapi/api.yaml +++ b/tests/fakeapi/api.yaml @@ -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 diff --git a/tests/fakeapi/hello.py b/tests/fakeapi/hello.py index 0d27208b3..6ee047954 100755 --- a/tests/fakeapi/hello.py +++ b/tests/fakeapi/hello.py @@ -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__ diff --git a/tests/test_app.py b/tests/test_app.py index e9fec30c6..05a814974 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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() == '""' + 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"'