Skip to content

Commit 8c82bf9

Browse files
committed
make fmt
1 parent 18af9e6 commit 8c82bf9

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,32 @@ def _get_body(self, app: EventHandlerInstance) -> dict[str, Any]:
255255

256256
content_type = app.current_event.headers.get("content-type", "").strip()
257257

258-
# Handle JSON content (default)
259-
if not content_type or content_type.startswith("application/json"):
258+
# If no content-type is provided, try to infer from route parameters
259+
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"
281+
282+
# Handle JSON content
283+
if content_type.startswith("application/json"):
260284
try:
261285
return app.current_event.json_body
262286
except json.JSONDecodeError as e:
@@ -301,7 +325,7 @@ def _parse_form_data(self, app: EventHandlerInstance) -> dict[str, Any]:
301325
parsed = parse_qs(body, keep_blank_values=True)
302326

303327
# Convert list values to single values where appropriate
304-
result = {}
328+
result: dict[str, Any] = {}
305329
for key, values in parsed.items():
306330
if len(values) == 1:
307331
result[key] = values[0]

0 commit comments

Comments
 (0)