github aws-powertools/powertools-lambda-python v3.27.0

9 hours ago

Summary

In this release, we focused on the Event Handler utility - we added three new features and shipped several important bug fixes across Event Handler and Idempotency.

  • File uploads - handle multipart/form-data uploads with full OpenAPI validation and Swagger UI file picker
  • Cookie parameters - use cookies as typed, validated parameters alongside Query(), Header(), and Form()
  • Request object - access the resolved route pattern, path parameters, and HTTP method in middleware and route handlers

A huge thanks to @oyiz-michael, @siwyd, @abhu85, and @danjhd for their contributions!

File upload support for OpenAPI

Docs

You can now handle file uploads in your API endpoints with full OpenAPI validation and Swagger UI support. If using Swagger, it renders a file picker automatically. A special thanks to @oyiz-michael for starting the initial work on this feature.

from typing import Annotated

from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.params import File, Form, UploadFile

app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(path="/swagger")


@app.post("/upload")
def upload(
    file_data: Annotated[UploadFile, File(description="CSV file")],
    separator: Annotated[str, Form(description="CSV separator")] = ",",
):
    return {
        "filename": file_data.filename,
        "content_type": file_data.content_type,
        "file_size": len(file_data),
    }

You can receive files as raw bytes (Annotated[bytes, File()]) or as an UploadFile object with filename and content type metadata.

Cookie parameter support for OpenAPI

Docs

You can now use cookies as typed, validated parameters in your API endpoints - just like Query(), Header(), or Form(). The OpenAPI schema generates in: cookie parameters automatically, and validation works across all resolver types.

from typing import Annotated

from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.params import Cookie

app = APIGatewayRestResolver(enable_validation=True)


@app.get("/me")
def get_me(
    session_id: Annotated[str, Cookie(description="Session identifier")],
    theme: Annotated[str, Cookie(description="UI theme")] = "light",
):
    return {"session_id": session_id, "theme": theme}

Request object for middleware and route handlers

Docs

We added a Request object that gives middleware and route handlers access to the resolved route pattern, Powertools-extracted path parameters, HTTP method, headers, query parameters, and body. Previously, middleware only had access to app.current_event, which returns raw API Gateway parameters (e.g. {"proxy": "users/123"} for {proxy+} routes) instead of the resolved ones.

You can access the Request object in two ways:

In middleware via app.request:

from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response
from aws_lambda_powertools.event_handler.middlewares import NextMiddleware

app = APIGatewayRestResolver()


def auth_middleware(app: APIGatewayRestResolver, next_middleware: NextMiddleware) -> Response:
    req = app.request
    route = req.route              # "/users/{user_id}"
    path_params = req.path_parameters  # {"user_id": "123"}
    method = req.method            # "GET"

    # auth logic here...
    return next_middleware(app)


app.use(middlewares=[auth_middleware])

In route handlers via type annotation:

from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Request

app = APIGatewayRestResolver()


@app.get("/users/<user_id>")
def get_user(user_id: str, request: Request):
    user_agent = request.headers.get("user-agent")
    return {"id": user_id, "route": request.route, "user_agent": user_agent}

Changes

  • feat(event_handler): add Cookie parameter support for OpenAPI utility (#8095) by @leandrodamascena
  • fix(data_classes): support {proxy+} and path parameters in authorizer response (#8092) by @leandrodamascena
  • fix(event_handler): sync middleware receives real response in async ASGI context (#8089) by @leandrodamascena
  • feat(event_handler): add Request object for middleware access to resolved route and args (#8036) by @oyiz-michael
  • fix(event_handler): support finding type annotated resolver when merging schemas (#8074) by @siwyd
  • fix(idempotency): serialize Pydantic models with mode='json' for UUID/date support (#8075) by @abhu85
  • fix(event_handler): normalize Union and RootModel sequences in body validation (#8067) by @danjhd

📜 Documentation updates

🔧 Maintenance

This release was made possible by the following contributors:

@abhu85, @danjhd, @dependabot[bot], @github-actions[bot], @leandrodamascena, @oyiz-michael, @ran-isenberg, @siwyd, dependabot[bot] and github-actions[bot]

Don't miss a new powertools-lambda-python release

NewReleases is sending notifications on new releases.