Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further improvements to path matching #64

Merged
merged 1 commit into from
Mar 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/'