@@ -300,8 +300,16 @@ def _validate_request_with_rust(
300300 if "Request body contains invalid JSON:" in str (exc ):
301301 raise orjson .JSONDecodeError ("invalid json" , "" , 0 ) from exc
302302 raise
303- except Exception :
304- raise
303+
304+ def _refresh_rust_validator_handles (self ) -> bool :
305+ """Populate cached Rust callables from the current validator instance."""
306+ if self ._rust_validator is None :
307+ return False
308+
309+ self ._rust_validate_http_request = self ._rust_validator .validate_http_request
310+ self ._rust_sanitize_response_body = self ._rust_validator .sanitize_response_body
311+ self ._rust_validate_resource_path = self ._rust_validator .validate_resource_path
312+ return True
305313
306314 def _validate_json_data_with_rust (self , data : Any ) -> tuple [str , str ] | None :
307315 """Validate JSON data with the Rust extension, falling back to Python on extension failures."""
@@ -385,12 +393,10 @@ def validate_resource_path(self, path: str) -> str:
385393 if getattr (settings , "experimental_rust_validation_middleware_enabled" , False ) is True :
386394 try :
387395 if self ._rust_validate_resource_path is None :
388- self ._rust_validator = self ._build_rust_validator ()
389- if self ._rust_validator is None :
396+ if not self ._refresh_rust_validator_handles ():
397+ self ._rust_validator = self ._build_rust_validator ()
398+ if not self ._refresh_rust_validator_handles ():
390399 return self ._validate_resource_path_with_python (path )
391- self ._rust_validate_http_request = self ._rust_validator .validate_http_request
392- self ._rust_sanitize_response_body = self ._rust_validator .sanitize_response_body
393- self ._rust_validate_resource_path = self ._rust_validator .validate_resource_path
394400
395401 return self ._rust_validate_resource_path (path )
396402 except ValueError as exc :
@@ -440,11 +446,9 @@ async def _sanitize_response(self, response: Response) -> Response:
440446 body = response .body
441447 if getattr (settings , "experimental_rust_validation_middleware_enabled" , False ) is True :
442448 if self ._rust_sanitize_response_body is None :
443- self ._rust_validator = self ._build_rust_validator ()
444- if self ._rust_validator is not None :
445- self ._rust_validate_http_request = self ._rust_validator .validate_http_request
446- self ._rust_sanitize_response_body = self ._rust_validator .sanitize_response_body
447- self ._rust_validate_resource_path = self ._rust_validator .validate_resource_path
449+ if not self ._refresh_rust_validator_handles ():
450+ self ._rust_validator = self ._build_rust_validator ()
451+ self ._refresh_rust_validator_handles ()
448452
449453 if self ._rust_sanitize_response_body is not None :
450454 if isinstance (body , str ):
0 commit comments