Skip to content

Commit 5e1a550

Browse files
committed
feat(openapi): add multipart/form-data & form support
1 parent f52cb20 commit 5e1a550

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

aws_lambda_powertools/event_handler/openapi/dependant.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ def get_body_field(*, dependant: Dependant, name: str) -> ModelField | None:
348348
alias="body",
349349
field_info=body_field_info(**body_field_info_kwargs),
350350
)
351+
351352
return final_field
352353

353354

@@ -367,11 +368,11 @@ def get_body_field_info(
367368
body_field_info_kwargs["default"] = None
368369

369370
if any(isinstance(f.field_info, _File) for f in flat_dependant.body_params):
370-
# MAINTENANCE: body_field_info: type[Body] = _File
371-
raise NotImplementedError("_File fields are not supported in request bodies")
371+
body_field_info = Body
372+
body_field_info_kwargs["media_type"] = "multipart/form-data"
372373
elif any(isinstance(f.field_info, _Form) for f in flat_dependant.body_params):
373-
# MAINTENANCE: body_field_info: type[Body] = _Form
374-
raise NotImplementedError("_Form fields are not supported in request bodies")
374+
body_field_info = Body
375+
body_field_info_kwargs["media_type"] = "application/x-www-form-urlencoded"
375376
else:
376377
body_field_info = Body
377378

aws_lambda_powertools/event_handler/openapi/models.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,7 @@ class OpenAPI(OpenAPIExtensions):
480480

481481
model_config = MODEL_CONFIG_ALLOW
482482

483-
# Helper schema for file upload
484-
class FileSchema(BaseModel):
485-
"""OpenAPI 3.0 schema for binary file uploads (multipart/form-data)."""
486483

487-
type: Literal["string"] = Field("string", const=True)
488-
format: Literal["binary"] = Field("binary", const=True)
489-
490-
model_config = MODEL_CONFIG_ALLOW
491484

492485

493486
model_rebuild(Schema)

aws_lambda_powertools/event_handler/openapi/params.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,13 @@ def __init__(
848848
json_schema_extra: dict[str, Any] | None = None,
849849
**extra: Any,
850850
):
851+
# For file uploads, ensure the OpenAPI schema has the correct format
852+
file_schema_extra = {"format": "binary"}
853+
if json_schema_extra:
854+
json_schema_extra.update(file_schema_extra)
855+
else:
856+
json_schema_extra = file_schema_extra
857+
851858
super().__init__(
852859
default=default,
853860
default_factory=default_factory,
@@ -1122,3 +1129,8 @@ def _create_model_field(
11221129
required=field_info.default in (Required, Undefined),
11231130
field_info=field_info,
11241131
)
1132+
1133+
1134+
# Public type aliases for form and file parameters
1135+
File = _File
1136+
Form = _Form

0 commit comments

Comments
 (0)