Skip to content

Commit 9a237d7

Browse files
committed
feat(event_handler): enhance OpenAPI response with headers, links, examples and encoding
- Add OpenAPIResponseHeader TypedDict with full OpenAPI spec compliance - Add headers and links fields to OpenAPIResponse TypedDict - Add examples and encoding fields to content models - Fix processing logic to preserve examples when using model field - Maintain 100% backward compatibility with total=False - Add comprehensive functional tests covering all scenarios Fixes aws-powertools#4870
1 parent 02b2305 commit 9a237d7

File tree

3 files changed

+492
-4
lines changed

3 files changed

+492
-4
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,16 @@ def _get_openapi_path( # noqa PLR0912
675675
if not return_field:
676676
raise AssertionError("Model declared in custom responses was not found")
677677

678-
new_payload = self._openapi_operation_return(
678+
model_payload = self._openapi_operation_return(
679679
param=return_field,
680680
model_name_map=model_name_map,
681681
field_mapping=field_mapping,
682682
)
683+
684+
# Preserve existing fields like examples, encoding, etc.
685+
new_payload = {**payload} # Copy all existing fields
686+
new_payload.update(model_payload) # Add/override with model schema
687+
new_payload.pop("model", None) # Remove the model field itself
683688

684689
# Case 2.2: the 'content' has a schema
685690
else:

aws_lambda_powertools/event_handler/openapi/types.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,31 @@
6363
}
6464

6565

66+
class OpenAPIResponseHeader(TypedDict, total=False):
67+
"""OpenAPI Response Header Object"""
68+
description: NotRequired[str]
69+
schema: NotRequired[dict[str, Any]]
70+
examples: NotRequired[dict[str, Any]]
71+
style: NotRequired[str]
72+
explode: NotRequired[bool]
73+
allowReserved: NotRequired[bool]
74+
deprecated: NotRequired[bool]
75+
76+
6677
class OpenAPIResponseContentSchema(TypedDict, total=False):
6778
schema: dict
79+
examples: NotRequired[dict[str, Any]]
80+
encoding: NotRequired[dict[str, Any]]
6881

6982

70-
class OpenAPIResponseContentModel(TypedDict):
83+
class OpenAPIResponseContentModel(TypedDict, total=False):
7184
model: Any
85+
examples: NotRequired[dict[str, Any]]
86+
encoding: NotRequired[dict[str, Any]]
7287

7388

74-
class OpenAPIResponse(TypedDict):
75-
description: str
89+
class OpenAPIResponse(TypedDict, total=False):
90+
description: str # Still required
91+
headers: NotRequired[dict[str, OpenAPIResponseHeader]]
7692
content: NotRequired[dict[str, OpenAPIResponseContentSchema | OpenAPIResponseContentModel]]
93+
links: NotRequired[dict[str, Any]]

0 commit comments

Comments
 (0)