Skip to content

Commit 4e897c7

Browse files
committed
fix: convert UploadFile to bytes for File() parameter validation
- Added UploadFile-to-bytes conversion in _normalize_field_value() - Handles type annotations including Annotated[bytes, File()] - Fixes 24 failing tests in test_file_parameter.py - All tests now return 200 OK instead of 422 validation errors Resolves multipart form data parsing issue where UploadFile instances weren't being converted to bytes before Pydantic validation.
1 parent d81b87f commit 4e897c7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,21 @@ def _handle_missing_field_value(
597597

598598
def _normalize_field_value(value: Any, field_info: FieldInfo) -> Any:
599599
"""Normalize field value, converting lists to single values for non-sequence fields."""
600+
# Convert UploadFile to bytes if the expected type is bytes
601+
if isinstance(value, UploadFile):
602+
# Check if the annotation is bytes
603+
annotation = field_info.annotation
604+
# Handle Annotated types by unwrapping
605+
if hasattr(annotation, "__origin__"):
606+
from typing import get_args
607+
608+
args = get_args(annotation)
609+
if args:
610+
annotation = args[0]
611+
612+
if annotation is bytes:
613+
return value.file
614+
600615
if field_annotation_is_sequence(field_info.annotation):
601616
return value
602617
elif isinstance(value, list) and value:

0 commit comments

Comments
 (0)