Skip to content

Commit bb80a2d

Browse files
Fix middleware validation
1 parent 8da7631 commit bb80a2d

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
_DEFAULT_OPENAPI_RESPONSE_DESCRIPTION = "Successful Response"
7474
_ROUTE_REGEX = "^{}$"
7575
_JSON_DUMP_CALL = partial(json.dumps, separators=(",", ":"), cls=Encoder)
76+
_DEFAULT_CONTENT_TYPE = "application/json"
7677

7778
ResponseEventT = TypeVar("ResponseEventT", bound=BaseProxyEvent)
7879
ResponseT = TypeVar("ResponseT")
@@ -265,7 +266,7 @@ def __init__(
265266
self,
266267
body: Any = None,
267268
status_code: int = 200,
268-
content_type: str = "application/json",
269+
content_type: str = _DEFAULT_CONTENT_TYPE,
269270
session_attributes: dict[str, Any] | None = None,
270271
prompt_session_attributes: dict[str, Any] | None = None,
271272
knowledge_bases_configuration: list[dict[str, Any]] | None = None,
@@ -329,7 +330,7 @@ def is_json(self) -> bool:
329330
content_type = self.headers.get("Content-Type", "")
330331
if isinstance(content_type, list):
331332
content_type = content_type[0]
332-
return content_type.startswith("application/json")
333+
return content_type.startswith(_DEFAULT_CONTENT_TYPE)
333334

334335

335336
class Route:
@@ -601,7 +602,7 @@ def _get_openapi_path(
601602
operation_responses: dict[int, OpenAPIResponse] = {
602603
422: {
603604
"description": "Validation Error",
604-
"content": {"application/json": {"schema": {"$ref": f"{COMPONENT_REF_PREFIX}HTTPValidationError"}}},
605+
"content": {_DEFAULT_CONTENT_TYPE: {"schema": {"$ref": f"{COMPONENT_REF_PREFIX}HTTPValidationError"}}},
605606
},
606607
}
607608

@@ -610,7 +611,9 @@ def _get_openapi_path(
610611
http_code = self.custom_response_validation_http_code.value
611612
operation_responses[http_code] = {
612613
"description": "Response Validation Error",
613-
"content": {"application/json": {"schema": {"$ref": f"{COMPONENT_REF_PREFIX}ResponseValidationError"}}},
614+
"content": {
615+
_DEFAULT_CONTENT_TYPE: {"schema": {"$ref": f"{COMPONENT_REF_PREFIX}ResponseValidationError"}},
616+
},
614617
}
615618
# Add model definition
616619
definitions["ResponseValidationError"] = response_validation_error_response_definition
@@ -623,7 +626,7 @@ def _get_openapi_path(
623626
# Case 1: there is not 'content' key
624627
if "content" not in response:
625628
response["content"] = {
626-
"application/json": self._openapi_operation_return(
629+
_DEFAULT_CONTENT_TYPE: self._openapi_operation_return(
627630
param=dependant.return_param,
628631
model_name_map=model_name_map,
629632
field_mapping=field_mapping,
@@ -674,7 +677,7 @@ def _get_openapi_path(
674677
# Add the response schema to the OpenAPI 200 response
675678
operation_responses[200] = {
676679
"description": self.response_description or _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
677-
"content": {"application/json": response_schema},
680+
"content": {_DEFAULT_CONTENT_TYPE: response_schema},
678681
}
679682

680683
operation["responses"] = operation_responses
@@ -1664,7 +1667,7 @@ def _add_resolver_response_validation_error_response_to_route(
16641667
response_validation_error_response = {
16651668
"description": "Response Validation Error",
16661669
"content": {
1667-
"application/json": {
1670+
_DEFAULT_CONTENT_TYPE: {
16681671
"schema": {"$ref": f"{COMPONENT_REF_PREFIX}ResponseValidationError"},
16691672
},
16701673
},
@@ -2183,7 +2186,7 @@ def swagger_handler():
21832186
if query_params.get("format") == "json":
21842187
return Response(
21852188
status_code=200,
2186-
content_type="application/json",
2189+
content_type=_DEFAULT_CONTENT_TYPE,
21872190
body=escaped_spec,
21882191
)
21892192

tests/functional/event_handler/_pydantic/test_bedrock_agent.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,22 @@ def claims() -> Dict[str, Any]:
284284
assert "knowledgeBasesConfiguration" not in result
285285

286286

287+
def test_bedrock_agent_with_string():
288+
# GIVEN a Bedrock Agent event
289+
app = BedrockAgentResolver()
290+
291+
@app.get("/claims", description="Gets claims")
292+
def claims() -> str:
293+
return "a"
294+
295+
# WHEN calling the event handler
296+
result = app(load_event("bedrockAgentEvent.json"), {})
297+
298+
# THEN process event correctly with only session_attributes
299+
assert result["messageVersion"] == "1.0"
300+
assert result["response"]["httpStatusCode"] == 200
301+
302+
287303
def test_bedrock_agent_with_different_attributes_combination():
288304
# GIVEN a Bedrock Agent event
289305
app = BedrockAgentResolver()

0 commit comments

Comments
 (0)