Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
openapi: Deduplicate last modified check.
Anders Kaseorg4990e6d4798c
diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.pyindex 8f26c485fbd..8c3fe3225dd 100644--- a/zerver/openapi/openapi.py+++ b/zerver/openapi/openapi.py@@ -28,7 +28,7 @@ class OpenAPISpec():self.core_data: Any = Noneself.documented_events: Set[str] = set()- def reload(self) -> None:+ def check_reload(self) -> None:# Because importing yamole (and in turn, yaml) takes# significant time, and we only use python-yaml for our API# docs, importing it lazily here is a significant optimization@@ -40,13 +40,20 @@ class OpenAPISpec():# only cause some extra processing at startup and not data# corruption.from yamole import YamoleParser++ last_modified = os.path.getmtime(self.path)+ # Using == rather than >= to cover the corner case of users placing an+ # earlier version than the current one+ if self.last_update == last_modified:+ return+with open(self.path) as f:yaml_parser = YamoleParser(f)self.data = yaml_parser.datavalidator_spec = create_spec(self.data)self.core_data = RequestValidator(validator_spec)self.create_regex_dict()- self.last_update = os.path.getmtime(self.path)+ self.last_update = last_modifieddef create_regex_dict(self) -> None:# Algorithm description:@@ -90,11 +97,7 @@ class OpenAPISpec():"""Reload the OpenAPI file if it has been modified after the last timeit was read, and then return the parsed data."""- last_modified = os.path.getmtime(self.path)- # Using != rather than < to cover the corner case of users placing an- # earlier version than the current one- if self.last_update != last_modified:- self.reload()+ self.check_reload()assert(len(self.data) > 0)return self.data@@ -102,11 +105,7 @@ class OpenAPISpec():"""Reload the OpenAPI file if it has been modified after the last timeit was read, and then return the parsed data."""- last_modified = os.path.getmtime(self.path)- # Using != rather than < to cover the corner case of users placing an- # earlier version than the current one- if self.last_update != last_modified:- self.reload()+ self.check_reload()assert(len(self.regex_dict) > 0)return self.regex_dict@@ -115,11 +114,7 @@ class OpenAPISpec():it was read, and then return the openapi_core validator object. Similarto preceding functions. Used for proper access to OpenAPI objects."""- last_modified = os.path.getmtime(self.path)- # Using != rather than < to cover the corner case of users placing an- # earlier version than the current one- if self.last_update != last_modified:- self.reload()+ self.check_reload()return self.core_dataclass SchemaError(Exception):diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.pyindex bb282a82d42..487b7d6637e 100644--- a/zerver/tests/test_openapi.py+++ b/zerver/tests/test_openapi.py@@ -196,9 +196,10 @@ class OpenAPIToolsTest(ZulipTestCase):self.assertNotEqual(openapi_spec.last_update, 0)# Now verify calling it again doesn't call reload- with mock.patch('zerver.openapi.openapi.openapi_spec.reload') as mock_reload:- get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)- self.assertFalse(mock_reload.called)+ old_spec_dict = openapi_spec.spec()+ get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)+ new_spec_dict = openapi_spec.spec()+ self.assertIs(old_spec_dict, new_spec_dict)class OpenAPIArgumentsTest(ZulipTestCase):# This will be filled during test_openapi_arguments: