Skip to content

Commit c94accc

Browse files
fix(bedrock_agent): fix querystring field resolution (#6777)
* fix: prevent splitting Bedrock Agent parameters with commas This fixes issue #6520 where Bedrock Agent parameters containing commas (like SQL queries) were being truncated because the resolved_query_string_parameters method was splitting them by commas. The fix overrides the resolved_query_string_parameters method in the BedrockAgentEvent class to preserve parameter values without splitting them by commas. * style: format test files * fix: address PR feedback for Bedrock Agent parameters with commas - Change @cached_property to @Property for resolved_query_string_parameters - Simplify the functional test by directly returning the query parameter - Remove redundant unit test --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 82e7fd5 commit c94accc

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

aws_lambda_powertools/utilities/data_classes/bedrock_agent_event.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def query_string_parameters(self) -> dict[str, str]:
112112
parameters = self.get("parameters") or []
113113
return {x["name"]: x["value"] for x in parameters}
114114

115+
@property
116+
def resolved_query_string_parameters(self) -> dict[str, list[str]]:
117+
"""
118+
Override the base implementation to prevent splitting parameter values by commas.
119+
120+
For Bedrock Agent events, parameters are already properly structured and should not
121+
be split by commas as they might contain commas as part of their actual values
122+
(e.g., SQL queries).
123+
"""
124+
# Return each parameter value as a single-item list without splitting by commas
125+
parameters = self.get("parameters") or []
126+
return {x["name"]: [x["value"]] for x in parameters}
127+
115128
@property
116129
def resolved_headers_field(self) -> dict[str, Any]:
117130
return {}

aws_lambda_powertools/utilities/data_classes/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def query_string_parameters(self) -> dict[str, str]:
168168
def multi_value_query_string_parameters(self) -> dict[str, list[str]]:
169169
return self.get("multiValueQueryStringParameters") or {}
170170

171-
@cached_property
171+
@property
172172
def resolved_query_string_parameters(self) -> dict[str, list[str]]:
173173
"""
174174
This property determines the appropriate query string parameter to be used

tests/functional/event_handler/_pydantic/test_bedrock_agent.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing_extensions import Annotated
66

77
from aws_lambda_powertools.event_handler import BedrockAgentResolver, BedrockResponse, Response, content_types
8-
from aws_lambda_powertools.event_handler.openapi.params import Body
8+
from aws_lambda_powertools.event_handler.openapi.params import Body, Query
99
from aws_lambda_powertools.utilities.data_classes import BedrockAgentEvent
1010
from tests.functional.utils import load_event
1111

@@ -343,3 +343,43 @@ def handler() -> Optional[Dict]:
343343

344344
# THEN the OpenAPI schema must contain the "x-requireConfirmation" extension at the operation level
345345
assert schema["paths"]["/"]["get"]["x-requireConfirmation"] == "ENABLED"
346+
347+
348+
def test_bedrock_agent_with_comma_parameters():
349+
# GIVEN a Bedrock Agent resolver
350+
app = BedrockAgentResolver()
351+
352+
@app.post("/sql-query", description="Run a SQL query")
353+
def run_sql_query(query: Annotated[str, Query()]):
354+
return {"result": query}
355+
356+
# WHEN calling the event handler with a parameter containing commas
357+
event = {
358+
"actionGroup": "TestActionGroup",
359+
"messageVersion": "1.0",
360+
"sessionId": "12345678912345",
361+
"sessionAttributes": {},
362+
"promptSessionAttributes": {},
363+
"inputText": "Run a SQL query",
364+
"agent": {
365+
"alias": "TEST",
366+
"name": "test",
367+
"version": "1",
368+
"id": "test123",
369+
},
370+
"httpMethod": "POST",
371+
"apiPath": "/sql-query",
372+
"parameters": [
373+
{
374+
"name": "query",
375+
"type": "string",
376+
"value": "SELECT a.source_name, b.thing FROM table",
377+
},
378+
],
379+
}
380+
381+
result = app(event, {})
382+
383+
# THEN the parameter with commas should be correctly passed to the handler
384+
body = json.loads(result["response"]["responseBody"]["application/json"]["body"])
385+
assert body["result"] == "SELECT a.source_name, b.thing FROM table"

0 commit comments

Comments
 (0)