@@ -142,13 +142,18 @@ async def _validate_request(self, request: Request):
142142 parameters_to_validate .append ((key , value ))
143143
144144 content_type = request .headers .get ("content-type" , "" )
145+ is_json_body = content_type .startswith ("application/json" )
146+
147+ if parameters_to_validate :
148+ self ._validate_parameters (parameters_to_validate )
149+
145150 body = b""
146- if content_type . startswith ( "application/json" ) :
151+ if is_json_body :
147152 body = await request .body ()
148153
149- if self ._rust_validate_http_request is not None :
154+ if self ._rust_validate_http_request is not None and is_json_body :
150155 try :
151- result = self ._validate_request_with_rust (parameters_to_validate , content_type , body )
156+ result = self ._validate_request_with_rust ([] , content_type , body )
152157 if result is not None :
153158 key , error_type = result
154159 self ._raise_validation_failure (key , error_type )
@@ -160,13 +165,7 @@ async def _validate_request(self, request: Request):
160165 except Exception as exc :
161166 logger .warning ("Rust validation extension unavailable or failed; falling back to Python validation: %s" , exc )
162167
163- if parameters_to_validate :
164- result = self ._validate_parameters_with_python (parameters_to_validate )
165- if result is not None :
166- key , error_type = result
167- self ._raise_validation_failure (key , error_type )
168-
169- if content_type .startswith ("application/json" ):
168+ if is_json_body :
170169 try :
171170 if body :
172171 data = orjson .loads (body )
@@ -299,7 +298,7 @@ def _validate_request_with_rust(
299298 if "maximum supported nesting depth" in str (exc ):
300299 raise HTTPException (status_code = 422 , detail = str (exc )) from exc
301300 if "Request body contains invalid JSON:" in str (exc ):
302- raise orjson .JSONDecodeError ("invalid json" , b "" , 0 ) from exc
301+ raise orjson .JSONDecodeError ("invalid json" , "" , 0 ) from exc
303302 raise
304303 except Exception :
305304 raise
@@ -324,39 +323,6 @@ def _validate_json_data_with_rust(self, data: Any) -> tuple[str, str] | None:
324323 logger .warning ("Rust validation extension unavailable or failed; falling back to Python validation: %s" , exc )
325324 return self ._validate_json_data_with_python (data )
326325
327- def _validate_json_body_with_rust (self , body : bytes ):
328- """Validate raw JSON bytes with the Rust extension, falling back to Python on extension failures."""
329- try :
330- if self ._rust_validator is None :
331- self ._rust_validator = self ._build_rust_validator ()
332- if self ._rust_validator is None :
333- data = orjson .loads (body )
334- self ._validate_json_data (data )
335- return
336-
337- result = self ._rust_validator .validate_json_bytes (body )
338- if result is not None :
339- key , error_type = result
340- self ._raise_validation_failure (key , error_type )
341- except ValueError as exc :
342- if "maximum supported nesting depth" in str (exc ):
343- raise HTTPException (status_code = 422 , detail = str (exc )) from exc
344- logger .warning ("Rust validation extension unavailable or failed; falling back to Python validation: %s" , exc )
345- data = orjson .loads (body )
346- result = self ._validate_json_data_with_python (data )
347- if result is not None :
348- key , error_type = result
349- self ._raise_validation_failure (key , error_type )
350- except HTTPException :
351- raise
352- except Exception as exc :
353- logger .warning ("Rust validation extension unavailable or failed; falling back to Python validation: %s" , exc )
354- data = orjson .loads (body )
355- result = self ._validate_json_data_with_python (data )
356- if result is not None :
357- key , error_type = result
358- self ._raise_validation_failure (key , error_type )
359-
360326 def _validate_json_data_with_python (self , data : Any , depth : int = 0 ) -> tuple [str , str ] | None :
361327 """Validate JSON data with the Python implementation."""
362328 if depth > _MAX_JSON_VALIDATION_DEPTH :
0 commit comments