Skip to content

Commit

Permalink
Merge pull request #37 from hjacobs/fix-paths-with-hyphen-issue-36
Browse files Browse the repository at this point in the history
Fix paths with hyphen issue 36
  • Loading branch information
jmcs committed Jul 17, 2015
2 parents 46c34a7 + 476cbe1 commit d81d117
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions connexion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"""

import importlib
import re

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


def flaskify_endpoint(identifier: str) -> str:
Expand All @@ -21,13 +24,19 @@ def flaskify_endpoint(identifier: str) -> str:
return identifier.replace('.', '_')


def convert_path_parameter(match):
return '<{}>'.format(match.group(1).replace('-', '_'))


def flaskify_path(swagger_path: str) -> str:
"""
Convert swagger path templates to flask path templates
>>> flaskify_path('/foo-bar/{my-param}')
'/foo-bar/<my_param>'
"""
translation_table = str.maketrans('{-}', '<_>')
# TODO add types
return swagger_path.translate(translation_table)
return PATH_PARAMETER.sub(convert_path_parameter, swagger_path)


def get_function_from_name(operation_id: str) -> str:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
def test_flaskify_path():
assert utils.flaskify_path("{test-path}") == "<test_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>"


def test_flaskify_endpoint():
Expand Down

0 comments on commit d81d117

Please sign in to comment.