Skip to content

Commit

Permalink
Merge pull request #64 from pipermerriam/piper/issues-58-improve-path…
Browse files Browse the repository at this point in the history
…-matching

Further improvements to path matching
  • Loading branch information
pipermerriam committed Mar 27, 2015
2 parents 4a710ce + ff1e120 commit 62507f6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
14 changes: 7 additions & 7 deletions flex/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,18 @@ def match_path_to_api_path(path_definitions, target_path, base_path='', global_p
raise LookupError(MESSAGES['path']['no_matching_paths_found'].format(target_path))
elif len(matching_api_paths) > 1:
# TODO: This area needs improved logic.
# We check to see if any of the matches has more matched groups than
# We check to see if any of the matched paths is longers than
# the others. If so, we *assume* it is the correct match. This is
# going to be prone to false positives. in certain cases.
matches_by_group_size = collections.defaultdict(list)
matches_by_path_size = collections.defaultdict(list)
for path, match in matching_api_paths:
matches_by_group_size[len(match.groups())].append(path)
longest_match = max(matches_by_group_size.keys())
if len(matches_by_group_size[longest_match]) == 1:
return matches_by_group_size[longest_match][0]
matches_by_path_size[len(path)].append(path)
longest_match = max(matches_by_path_size.keys())
if len(matches_by_path_size[longest_match]) == 1:
return matches_by_path_size[longest_match][0]
raise MultiplePathsFound(
MESSAGES['path']['multiple_paths_found'].format(
[v[0] for v in matching_api_paths]
target_path, [v[0] for v in matching_api_paths],
)
)
else:
Expand Down
42 changes: 41 additions & 1 deletion tests/core/test_match_request_path_to_api_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ def test_matching_with_nested_path():
)
assert path == '/get/{id}/nested/{other_id}'

def test_matching_with_full_nested_path():

def test_matching_with_full_nested_parametrized_resource():
schema = SchemaFactory(
paths={
'/get/main/':{
Expand Down Expand Up @@ -251,3 +252,42 @@ def test_matching_with_full_nested_path():
target_path='/get/main/1234/nested/5678',
)
assert path == '/get/main/{id}/nested/{other_id}'


def test_matching_with_full_nested_list_resource():
schema = SchemaFactory(
paths={
'/get/main/':{
'get':{},
},
'/get/main/{id}': {
'get': {
'parameters': [{
'name': 'id',
'in': PATH,
'type': STRING,
'required': True,
}],
},
},
'/get/main/{id}/nested/': {
'get': {
'parameters': [
{
'name': 'id',
'in': PATH,
'type': STRING,
'required': True,
},
],
},
},
},
)
paths = schema['paths']

path = match_path_to_api_path(
path_definitions=paths,
target_path='/get/main/1234/nested/',
)
assert path == '/get/main/{id}/nested/'

0 comments on commit 62507f6

Please sign in to comment.