From a52995c74814c916f4fd342a06a0788e2a37be74 Mon Sep 17 00:00:00 2001 From: Facundo Lucero Date: Sat, 27 Dec 2025 12:32:22 -0300 Subject: [PATCH] fix(event_handler): preserve openapi_examples on Body --- .../event_handler/api_gateway.py | 2 + .../event_handler/openapi/params.py | 4 ++ .../_pydantic/test_openapi_params.py | 39 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index d31a24e74fb..1b34c7b1698 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -826,6 +826,8 @@ def _openapi_operation_request_body( # Generate the request body media type request_media_content: dict[str, Any] = {"schema": body_schema} + if field_info.openapi_examples: + request_media_content["examples"] = field_info.openapi_examples request_body_oai["content"] = {request_media_type: request_media_content} return request_body_oai diff --git a/aws_lambda_powertools/event_handler/openapi/params.py b/aws_lambda_powertools/event_handler/openapi/params.py index 0439b1c2fc1..be4256703ba 100644 --- a/aws_lambda_powertools/event_handler/openapi/params.py +++ b/aws_lambda_powertools/event_handler/openapi/params.py @@ -716,8 +716,12 @@ def __init__( ) if examples is not None: kwargs["examples"] = examples + if openapi_examples is not None: + kwargs["openapi_examples"] = openapi_examples current_json_schema_extra = json_schema_extra or extra + self.openapi_examples = openapi_examples + kwargs.update( { "annotation": annotation, diff --git a/tests/functional/event_handler/_pydantic/test_openapi_params.py b/tests/functional/event_handler/_pydantic/test_openapi_params.py index 4c9087fff13..7efba0e16b3 100644 --- a/tests/functional/event_handler/_pydantic/test_openapi_params.py +++ b/tests/functional/event_handler/_pydantic/test_openapi_params.py @@ -567,6 +567,45 @@ def handler(user: Annotated[User, Body(description="This is a user")]): assert request_body.content[JSON_CONTENT_TYPE].schema_.description == "This is a user" +def test_openapi_with_body_examples(): + app = APIGatewayRestResolver() + + first_example = Example(summary="Example1", description="Example1", value={"name": "Alice"}) + second_example = Example(summary="Example2", description="Example2", value={"name": "Bob"}) + + class User(BaseModel): + name: str + + @app.post("/users") + def handler( + user: Annotated[ + User, + Body( + openapi_examples={ + "example1": first_example, + "example2": second_example, + }, + ), + ], + ): + print(user) + + schema = app.get_openapi_schema() + assert len(schema.paths.keys()) == 1 + + post = schema.paths["/users"].post + assert post.parameters is None + assert post.requestBody is not None + + request_body = post.requestBody + + # Examples should appear in the request_body content schema + request_body_examples = request_body.content[JSON_CONTENT_TYPE].examples + + assert request_body_examples["example1"] == first_example + assert request_body_examples["example2"] == second_example + + def test_openapi_with_deprecated_operations(): app = APIGatewayRestResolver()