Skip to content

Commit 1c5f1a1

Browse files
Merge branch 'develop' into feat/circuit-breaker
2 parents 6853f4b + f7239b8 commit 1c5f1a1

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

aws_lambda_powertools/event_handler/bedrock_agent.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ def lambda_handler(event, context):
9999

100100
current_event: BedrockAgentEvent
101101

102-
def __init__(self, debug: bool = False, enable_validation: bool = True):
102+
def __init__(
103+
self,
104+
debug: bool = False,
105+
enable_validation: bool = True,
106+
serializer: Callable[[dict], str] | None = None,
107+
):
103108
super().__init__(
104109
proxy_type=ProxyEventType.BedrockAgentEvent,
105110
cors=None,
106111
debug=debug,
107-
serializer=None,
112+
serializer=serializer,
108113
strip_prefixes=None,
109114
enable_validation=enable_validation,
110115
json_body_deserializer=None,

docs/core/event_handler/api_gateway.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,33 @@ Use `dependency_overrides` to replace any dependency with a mock or stub during
14591459
--8<-- "examples/event_handler_rest/src/dependency_injection_testing.py"
14601460
```
14611461

1462+
???+ warning "Import path must match exactly when using dependency_overrides"
1463+
When using `dependency_overrides` in tests, the imported dependency reference must use the **exact same import path** as the handler file. Python treats differently-imported modules as different objects in `sys.modules`, so the override will be silently ignored otherwise.
1464+
1465+
=== "✅ Correct"
1466+
1467+
```python
1468+
# handler.py
1469+
from depends import get_config
1470+
1471+
# test_handler.py - matches handler's import path exactly
1472+
from depends import get_config
1473+
1474+
app.dependency_overrides[get_config] = lambda: "test-value"
1475+
```
1476+
1477+
=== "❌ Wrong"
1478+
1479+
```python
1480+
# handler.py
1481+
from depends import get_config
1482+
1483+
# test_handler.py - different import path, override won't apply
1484+
from my_app.api_handler.depends import get_config
1485+
1486+
app.dependency_overrides[get_config] = lambda: "test-value"
1487+
```
1488+
14621489
???+ tip "Caching behavior"
14631490
By default, dependencies are cached within the same invocation (`use_cache=True`). If the same dependency is used by multiple handlers or sub-dependencies, it is resolved once and the result is reused. Use `Depends(fn, use_cache=False)` to resolve every time.
14641491

tests/functional/event_handler/_pydantic/test_bedrock_agent.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from functools import partial
23
from typing import Any, Dict, Optional
34

45
import pytest
@@ -383,3 +384,37 @@ def run_sql_query(query: Annotated[str, Query()]):
383384
# THEN the parameter with commas should be correctly passed to the handler
384385
body = json.loads(result["response"]["responseBody"]["application/json"]["body"])
385386
assert body["result"] == "SELECT a.source_name, b.thing FROM table"
387+
388+
389+
def test_bedrock_agent_with_default_serializer_escapes_non_ascii():
390+
# GIVEN a Bedrock Agent resolver using the default serializer
391+
app = BedrockAgentResolver()
392+
393+
@app.get("/claims", description="Gets claims")
394+
def claims() -> Dict[str, Any]:
395+
return {"output": "잔액은 1,000원입니다 💰"}
396+
397+
# WHEN calling the event handler
398+
result = app(load_event("bedrockAgentEvent.json"), {})
399+
400+
# THEN the body is valid JSON and non-ASCII characters are escaped (default json.dumps behavior)
401+
body = result["response"]["responseBody"]["application/json"]["body"]
402+
assert "\\uc794" in body # "잔" escaped
403+
assert json.loads(body) == {"output": "잔액은 1,000원입니다 💰"}
404+
405+
406+
def test_bedrock_agent_with_custom_serializer_preserves_non_ascii():
407+
# GIVEN a Bedrock Agent resolver initialized with a custom serializer that keeps non-ASCII characters
408+
app = BedrockAgentResolver(serializer=partial(json.dumps, ensure_ascii=False))
409+
410+
@app.get("/claims", description="Gets claims")
411+
def claims() -> Dict[str, Any]:
412+
return {"output": "잔액은 1,000원입니다 💰"}
413+
414+
# WHEN calling the event handler
415+
result = app(load_event("bedrockAgentEvent.json"), {})
416+
417+
# THEN the non-ASCII characters are preserved verbatim in the response body
418+
body = result["response"]["responseBody"]["application/json"]["body"]
419+
assert "잔액은 1,000원입니다 💰" in body
420+
assert json.loads(body) == {"output": "잔액은 1,000원입니다 💰"}

0 commit comments

Comments
 (0)