Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
openapi_py: Add openapi-core validator object to OpenAPISpec class.
orientorbff8e43729ba
diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.pyindex 7479689c387..2be2eaf3a4b 100644--- a/zerver/openapi/openapi.py+++ b/zerver/openapi/openapi.py@@ -4,6 +4,9 @@ import osimport refrom typing import Any, Dict, List, Optional, Set+from openapi_core import create_spec+from openapi_core.testing import MockRequest+from openapi_core.validation.request.validators import RequestValidatorfrom openapi_schema_validator import OAS30ValidatorOPENAPI_SPEC_PATH = os.path.abspath(os.path.join(@@ -12,7 +15,7 @@ OPENAPI_SPEC_PATH = os.path.abspath(os.path.join(# A list of endpoint-methods such that the endpoint# has documentation but not with this particular method.-EXCLUDE_UNDOCUMENTED_ENDPOINTS = {"/realm/emoji/{emoji_name}:delete"}+EXCLUDE_UNDOCUMENTED_ENDPOINTS = {"/realm/emoji/{emoji_name}:delete", "/users:patch"}# Consists of endpoints with some documentation remaining.# These are skipped but return true as the validator cannot exclude objectsEXCLUDE_DOCUMENTED_ENDPOINTS = {"/events:get", "/register:post", "/settings/notifications:patch"}@@ -22,6 +25,7 @@ class OpenAPISpec():self.last_update: Optional[float] = Noneself.data: Dict[str, Any] = {}self.regex_dict: Dict[str, str] = {}+ self.core_data: Any = Nonedef reload(self) -> None:# Because importing yamole (and in turn, yaml) takes@@ -37,8 +41,9 @@ class OpenAPISpec():from yamole import YamoleParserwith open(self.path) as f:yaml_parser = YamoleParser(f)-self.data = yaml_parser.data+ validator_spec = create_spec(self.data)+ self.core_data = RequestValidator(validator_spec)self.create_regex_dict()self.last_update = os.path.getmtime(self.path)@@ -104,6 +109,17 @@ class OpenAPISpec():assert(len(self.regex_dict) > 0)return self.regex_dict+ def core_validator(self) -> Any:+ """Reload the OpenAPI file if it has been modified after the last time+ it was read, and then return the openapi_core validator object. Similar+ to 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()+ return self.core_dataclass SchemaError(Exception):pass