Skip to content

Commit

Permalink
#104 support int/float path converter
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Dec 5, 2015
1 parent 1d8f758 commit 0c56d15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 18 additions & 5 deletions connexion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

PATH_PARAMETER = re.compile(r'\{([^}]*)\}')

# map Swagger type to flask path converter
# see http://flask.pocoo.org/docs/0.10/api/#url-route-registrations
PATH_PARAMETER_CONVERTERS = {
'integer': 'int',
'number': 'float'
}


def flaskify_endpoint(identifier):
"""
Expand All @@ -29,11 +36,14 @@ def flaskify_endpoint(identifier):
return identifier.replace('.', '_')


def convert_path_parameter(match):
return '<{}>'.format(match.group(1).replace('-', '_'))
def convert_path_parameter(match, types):
name = match.group(1)
swagger_type = types.get(name)
converter = PATH_PARAMETER_CONVERTERS.get(swagger_type)
return '<{}{}{}>'.format(converter or '', ':' if converter else '', name.replace('-', '_'))


def flaskify_path(swagger_path):
def flaskify_path(swagger_path, types={}):
"""
Convert swagger path templates to flask path templates
Expand All @@ -42,9 +52,12 @@ def flaskify_path(swagger_path):
>>> flaskify_path('/foo-bar/{my-param}')
'/foo-bar/<my_param>'
>>> flaskify_path('/foo/{someint}', {'someint': 'int'})
'/foo/<int:someint>'
"""
# TODO add types
return PATH_PARAMETER.sub(convert_path_parameter, swagger_path)
convert_match = functools.partial(convert_path_parameter, types=types)
return PATH_PARAMETER.sub(convert_match, swagger_path)


def deep_getattr(obj, attr):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def test_flaskify_path():
assert utils.flaskify_path("api/{test-path}") == "api/<test_path>"
assert utils.flaskify_path("my-api/{test-path}") == "my-api/<test_path>"
assert utils.flaskify_path("foo_bar/{a-b}/{c_d}") == "foo_bar/<a_b>/<c_d>"
assert utils.flaskify_path("foo/{a}/{b}", {'a': 'integer'}) == "foo/<int:a>/<b>"
assert utils.flaskify_path("foo/{a}/{b}", {'a': 'number'}) == "foo/<float:a>/<b>"


def test_flaskify_endpoint():
Expand Down

0 comments on commit 0c56d15

Please sign in to comment.