From ff1e1205e89845aeb0449442218636d23361fcf9 Mon Sep 17 00:00:00 2001 From: Piper Merriam Date: Fri, 27 Mar 2015 07:57:51 -0600 Subject: [PATCH] Further improvements to path matching --- flex/paths.py | 14 +++---- .../test_match_request_path_to_api_path.py | 42 ++++++++++++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/flex/paths.py b/flex/paths.py index 5499ebc..2a5f106 100644 --- a/flex/paths.py +++ b/flex/paths.py @@ -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: diff --git a/tests/core/test_match_request_path_to_api_path.py b/tests/core/test_match_request_path_to_api_path.py index 64b20c0..36b176c 100644 --- a/tests/core/test_match_request_path_to_api_path.py +++ b/tests/core/test_match_request_path_to_api_path.py @@ -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/':{ @@ -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/'