Skip to content

Commit 5f2e22e

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

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

aws_lambda_powertools/event_handler/openapi/params.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,5 +1132,6 @@ def _create_model_field(
11321132

11331133

11341134
# Public type aliases for form and file parameters
1135-
File = _File
1136-
Form = _Form
1135+
# Use Annotated types to work properly with Pydantic
1136+
File = Annotated[bytes, _File()]
1137+
Form = Annotated[str, _Form()]

docs/core/event_handler/api_gateway.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,81 @@ In the following example, we use a new `Header` OpenAPI type to add [one out of
523523

524524
1. `cloudfront_viewer_country` is a list that must contain values from the `CountriesAllowed` enumeration.
525525

526+
#### Handling file uploads and form data
527+
528+
!!! info "You must set `enable_validation=True` to handle file uploads and form data via type annotation."
529+
530+
We use the `Annotated` type to tell the Event Handler that a parameter expects file upload or form data. This automatically sets the correct OpenAPI schema for `multipart/form-data` requests.
531+
532+
In the following example, we use `File` and `Form` OpenAPI types to handle file uploads and form fields:
533+
534+
* `File` parameters expect binary file data and generate OpenAPI schema with `format: binary`
535+
* `Form` parameters expect form field values from multipart form data
536+
* The OpenAPI spec will automatically set `requestBody` content type to `multipart/form-data`
537+
538+
=== "handling_file_uploads.py"
539+
540+
```python hl_lines="5 9-10 18-19"
541+
from typing import Annotated
542+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
543+
from aws_lambda_powertools.event_handler.openapi.params import File, Form
544+
545+
app = APIGatewayRestResolver(enable_validation=True)
546+
547+
@app.post("/upload")
548+
def upload_file(
549+
file: Annotated[bytes, File(description="File to upload")],
550+
filename: Annotated[str, Form(description="Name of the file")]
551+
):
552+
# file contains the binary data of the uploaded file
553+
# filename contains the form field value
554+
return {
555+
"message": f"Uploaded {filename}",
556+
"size": len(file)
557+
}
558+
559+
def lambda_handler(event, context):
560+
return app.resolve(event, context)
561+
```
562+
563+
1. If you're not using Python 3.9 or higher, you can install and use [`typing_extensions`](https://pypi.org/project/typing-extensions/){target="_blank" rel="nofollow"} to the same effect
564+
2. `File` is a special OpenAPI type for binary file uploads that sets `format: binary` in the schema
565+
3. `Form` is a special OpenAPI type for form field values in multipart requests
566+
567+
=== "Multiple files"
568+
569+
You can handle multiple file uploads by declaring parameters as lists:
570+
571+
```python hl_lines="9-10"
572+
from typing import Annotated, List
573+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
574+
from aws_lambda_powertools.event_handler.openapi.params import File, Form
575+
576+
app = APIGatewayRestResolver(enable_validation=True)
577+
578+
@app.post("/upload-multiple")
579+
def upload_multiple_files(
580+
files: Annotated[List[bytes], File(description="Files to upload")],
581+
description: Annotated[str, Form(description="Upload description")]
582+
):
583+
return {
584+
"message": f"Uploaded {len(files)} files",
585+
"description": description,
586+
"total_size": sum(len(file) for file in files)
587+
}
588+
```
589+
590+
1. `files` will be a list containing the binary data of each uploaded file
591+
592+
???+ note "OpenAPI Schema Generation"
593+
When you use `File` or `Form` parameters, the generated OpenAPI specification will automatically include:
594+
595+
* `requestBody` with content type `multipart/form-data`
596+
* Proper schema definitions with `format: binary` for file parameters
597+
* Form field descriptions and constraints
598+
599+
This ensures API documentation tools like SwaggerUI correctly display file upload interfaces.
600+
526601
#### Supported types for response serialization
527602

528603
With data validation enabled, we natively support serializing the following data types to JSON:

0 commit comments

Comments
 (0)