3333
3434# Constants
3535CONTENT_DISPOSITION_NAME_PARAM = "name="
36+ APPLICATION_JSON_CONTENT_TYPE = "application/json"
3637
3738
3839class OpenAPIValidationMiddleware (BaseMiddlewareHandler ):
@@ -252,50 +253,15 @@ def _get_body(self, app: EventHandlerInstance) -> dict[str, Any]:
252253 """
253254 Get the request body from the event, and parse it according to content type.
254255 """
255-
256256 content_type = app .current_event .headers .get ("content-type" , "" ).strip ()
257257
258258 # If no content-type is provided, try to infer from route parameters
259259 if not content_type :
260- route = app .context .get ("_route" )
261- if route and route .dependant .body_params :
262- # Check if any body params are File or Form types
263- from aws_lambda_powertools .event_handler .openapi .params import File , Form
264-
265- has_file_params = any (
266- isinstance (getattr (param .field_info , "__class__" , None ), type )
267- and issubclass (param .field_info .__class__ , (File , Form ))
268- for param in route .dependant .body_params
269- if hasattr (param , "field_info" )
270- )
271-
272- if has_file_params :
273- # Default to multipart for File/Form parameters
274- content_type = "multipart/form-data"
275- else :
276- # Default to JSON for other body parameters
277- content_type = "application/json"
278- else :
279- # Default to JSON when no body params
280- content_type = "application/json"
260+ content_type = self ._infer_content_type (app )
281261
282262 # Handle JSON content
283- if content_type .startswith ("application/json" ):
284- try :
285- return app .current_event .json_body
286- except json .JSONDecodeError as e :
287- raise RequestValidationError (
288- [
289- {
290- "type" : "json_invalid" ,
291- "loc" : ("body" , e .pos ),
292- "msg" : "JSON decode error" ,
293- "input" : {},
294- "ctx" : {"error" : e .msg },
295- },
296- ],
297- body = e .doc ,
298- ) from e
263+ if content_type .startswith (APPLICATION_JSON_CONTENT_TYPE ):
264+ return self ._parse_json_data (app )
299265
300266 # Handle URL-encoded form data
301267 elif content_type .startswith ("application/x-www-form-urlencoded" ):
@@ -317,6 +283,43 @@ def _get_body(self, app: EventHandlerInstance) -> dict[str, Any]:
317283 ],
318284 )
319285
286+ def _infer_content_type (self , app : EventHandlerInstance ) -> str :
287+ """Infer content type from route parameters when not explicitly provided."""
288+ route = app .context .get ("_route" )
289+ if route and route .dependant .body_params :
290+ # Check if any body params are File or Form types
291+ from aws_lambda_powertools .event_handler .openapi .params import File , Form
292+
293+ has_file_params = any (
294+ isinstance (getattr (param .field_info , "__class__" , None ), type )
295+ and issubclass (param .field_info .__class__ , (File , Form ))
296+ for param in route .dependant .body_params
297+ if hasattr (param , "field_info" )
298+ )
299+
300+ return "multipart/form-data" if has_file_params else APPLICATION_JSON_CONTENT_TYPE
301+
302+ # Default to JSON when no body params
303+ return APPLICATION_JSON_CONTENT_TYPE
304+
305+ def _parse_json_data (self , app : EventHandlerInstance ) -> dict [str , Any ]:
306+ """Parse JSON data from the request body."""
307+ try :
308+ return app .current_event .json_body
309+ except json .JSONDecodeError as e :
310+ raise RequestValidationError (
311+ [
312+ {
313+ "type" : "json_invalid" ,
314+ "loc" : ("body" , e .pos ),
315+ "msg" : "JSON decode error" ,
316+ "input" : {},
317+ "ctx" : {"error" : e .msg },
318+ },
319+ ],
320+ body = e .doc ,
321+ ) from e
322+
320323 def _parse_form_data (self , app : EventHandlerInstance ) -> dict [str , Any ]:
321324 """Parse URL-encoded form data from the request body."""
322325 try :
0 commit comments