Skip to content

Commit e79eef4

Browse files
authored
fix(event_handler): validate POST bodies on BedrockAgentResolver (#3903)
1 parent ca2e718 commit e79eef4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

aws_lambda_powertools/utilities/data_classes/bedrock_agent_event.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import cached_property
12
from typing import Any, Dict, List, Optional
23

34
from aws_lambda_powertools.utilities.data_classes.common import BaseProxyEvent, DictWrapper
@@ -112,3 +113,16 @@ def query_string_parameters(self) -> Optional[Dict[str, str]]:
112113
@property
113114
def resolved_headers_field(self) -> Optional[Dict[str, Any]]:
114115
return {}
116+
117+
@cached_property
118+
def json_body(self) -> Any:
119+
# In Bedrock Agent events, body parameters are encoded differently
120+
# @see https://docs.aws.amazon.com/bedrock/latest/userguide/agents-lambda.html#agents-lambda-input
121+
if not self.request_body:
122+
return None
123+
124+
json_body = self.request_body.content.get("application/json")
125+
if not json_body:
126+
return None
127+
128+
return {x.name: x.value for x in json_body.properties}

tests/functional/event_handler/test_bedrock_agent.py

+27
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from typing import Any, Dict
33

44
from aws_lambda_powertools.event_handler import BedrockAgentResolver, Response, content_types
5+
from aws_lambda_powertools.event_handler.openapi.params import Body
56
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
7+
from aws_lambda_powertools.shared.types import Annotated
68
from aws_lambda_powertools.utilities.data_classes import BedrockAgentEvent
79
from tests.functional.utils import load_event
810

@@ -157,3 +159,28 @@ def claims():
157159

158160
body = result["response"]["responseBody"]["text/plain"]["body"]
159161
assert body == "Something went wrong"
162+
163+
164+
def test_bedrock_agent_with_post():
165+
# GIVEN a Bedrock Agent resolver with a POST method
166+
app = BedrockAgentResolver()
167+
168+
@app.post("/send-reminders", description="Sends reminders")
169+
def send_reminders(
170+
_claim_id: Annotated[int, Body(description="Claim ID", alias="claimId")],
171+
_pending_documents: Annotated[str, Body(description="Social number and VAT", alias="pendingDocuments")],
172+
) -> Annotated[bool, Body(description="returns true if I like the email")]:
173+
return True
174+
175+
# WHEN calling the event handler
176+
result = app(load_event("bedrockAgentPostEvent.json"), {})
177+
178+
# THEN process the event correctly
179+
assert result["messageVersion"] == "1.0"
180+
assert result["response"]["apiPath"] == "/send-reminders"
181+
assert result["response"]["httpMethod"] == "POST"
182+
assert result["response"]["httpStatusCode"] == 200
183+
184+
# THEN return the correct result
185+
body = result["response"]["responseBody"]["application/json"]["body"]
186+
assert json.loads(body) is True

0 commit comments

Comments
 (0)