Skip to content

Commit da44588

Browse files
committed
Resets import path on each request
Ensures the import path is reset before resolving a new request. This prevents state from previous requests from polluting subsequent request resolution, particularly when using dynamic routing and directory mode. A new test case has also been added to verify this behavior.
1 parent 7d9220b commit da44588

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

acai_aws/apigateway/resolver/modes/directory.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
class DirectoryModeResolver(BaseModeResolver):
66

7+
INIT_FILE = '__init__.py'
8+
79
def __init__(self, **kwargs):
810
super().__init__(**kwargs)
911
self.__handler_path = self.importer.clean_path(kwargs['handlers'])
1012

1113
def _get_file_and_import_path(self, request_path):
14+
# ensure previous lookups don't leave behind path/dynamic state
15+
self.reset()
1216
split_path = self.get_request_path_as_list(request_path)
1317
route_path = self.__get_route_path(split_path)
1418
file_path = self.__handler_path + self.importer.file_separator + route_path
@@ -25,7 +29,7 @@ def __get_import_path_file_tree(self, split_path, split_index, file_tree):
2529
part = split_path[split_index]
2630
if part == '':
2731
possible_directory = None
28-
possible_file = '__init__.py'
32+
possible_file = self.INIT_FILE
2933
else:
3034
possible_directory = part.replace('-', '_')
3135
possible_file = f'{possible_directory}.py'
@@ -44,7 +48,7 @@ def __handle_directory_path_part(self, possible_directory, split_path, split_ind
4448
file_leaf = self.determine_which_file_leaf(file_tree, possible_directory)
4549
self.__get_import_path_file_tree(split_path, split_index+1, file_leaf)
4650
else:
47-
self.append_import_path('__init__.py')
51+
self.append_import_path(self.INIT_FILE)
4852

4953
def __handle_file_path_part(self, possible_file, split_path, split_index, file_tree):
5054
self.append_import_path(possible_file)
@@ -55,7 +59,7 @@ def __handle_dynamic_path_part(self, split_path, split_index, file_tree):
5559
file_part = list(file_tree['__dynamic_files'])[0]
5660
self.append_import_path(file_part)
5761
if '.py' not in file_part and split_index+1 == len(split_path):
58-
self.append_import_path('__init__.py')
62+
self.append_import_path(self.INIT_FILE)
5963
file_leaf = self.determine_which_file_leaf(file_tree, file_part)
6064
self.has_dynamic_route = True
6165
self.dynamic_parts[split_index] = split_path[split_index]

tests/acai_aws/apigateway/resolver/modes/test_directory.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,10 @@ def test_triple_nested_dynamic_get_file_and_import_path(self):
121121
file_path, import_path = self.directory_resolver._get_file_and_import_path(request.path)
122122
self.assertTrue('tests/mocks/apigateway/resolver/directory_handlers/user/_user_id/item/_item_id.py' in file_path)
123123
self.assertEqual('tests.mocks.apigateway.resolver.directory_handlers.user._user_id.item._item_id', import_path)
124+
125+
def test_repeated_directory_request_resets_import_path(self):
126+
request = Request(self.init_request)
127+
first_file_path, first_import_path = self.directory_resolver._get_file_and_import_path(request.path)
128+
second_file_path, second_import_path = self.directory_resolver._get_file_and_import_path(request.path)
129+
self.assertEqual(first_file_path, second_file_path)
130+
self.assertEqual(first_import_path, second_import_path)

0 commit comments

Comments
 (0)