Skip to content

Commit 305106e

Browse files
feat(event_handler): adding status_code OpenAPI field
1 parent 3838a42 commit 305106e

4 files changed

Lines changed: 23 additions & 19 deletions

File tree

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
2727
DEFAULT_OPENAPI_TITLE,
2828
DEFAULT_OPENAPI_VERSION,
29+
DEFAULT_STATUS_CODE,
2930
)
3031
from aws_lambda_powertools.event_handler.openapi.exceptions import (
3132
RequestUnsupportedContentType,
@@ -283,7 +284,7 @@ class BedrockResponse(Generic[ResponseT]):
283284
def __init__(
284285
self,
285286
body: Any = None,
286-
status_code: int = 200,
287+
status_code: int = DEFAULT_STATUS_CODE,
287288
content_type: str = DEFAULT_CONTENT_TYPE,
288289
session_attributes: dict[str, Any] | None = None,
289290
prompt_session_attributes: dict[str, Any] | None = None,
@@ -387,7 +388,7 @@ def __init__(
387388
deprecated: bool = False,
388389
enable_validation: bool | None = None,
389390
custom_response_validation_http_code: HTTPStatus | None = None,
390-
status_code: int = 200,
391+
status_code: int = DEFAULT_STATUS_CODE,
391392
middlewares: list[Callable[..., Response]] | None = None,
392393
):
393394
"""
@@ -814,7 +815,7 @@ def route(
814815
deprecated: bool = False,
815816
enable_validation: bool | None = None,
816817
custom_response_validation_http_code: int | HTTPStatus | None = None,
817-
status_code: int = 200,
818+
status_code: int = DEFAULT_STATUS_CODE,
818819
middlewares: list[Callable[..., Any]] | None = None,
819820
) -> Callable[[AnyCallableT], AnyCallableT]:
820821
raise NotImplementedError()
@@ -878,7 +879,7 @@ def get(
878879
deprecated: bool = False,
879880
enable_validation: bool | None = None,
880881
custom_response_validation_http_code: int | HTTPStatus | None = None,
881-
status_code: int = 200,
882+
status_code: int = DEFAULT_STATUS_CODE,
882883
middlewares: list[Callable[..., Any]] | None = None,
883884
) -> Callable[[AnyCallableT], AnyCallableT]:
884885
"""Get route decorator with GET `method`
@@ -943,7 +944,7 @@ def post(
943944
deprecated: bool = False,
944945
enable_validation: bool | None = None,
945946
custom_response_validation_http_code: int | HTTPStatus | None = None,
946-
status_code: int = 200,
947+
status_code: int = DEFAULT_STATUS_CODE,
947948
middlewares: list[Callable[..., Any]] | None = None,
948949
) -> Callable[[AnyCallableT], AnyCallableT]:
949950
"""Post route decorator with POST `method`
@@ -1009,7 +1010,7 @@ def put(
10091010
deprecated: bool = False,
10101011
enable_validation: bool | None = None,
10111012
custom_response_validation_http_code: int | HTTPStatus | None = None,
1012-
status_code: int = 200,
1013+
status_code: int = DEFAULT_STATUS_CODE,
10131014
middlewares: list[Callable[..., Any]] | None = None,
10141015
) -> Callable[[AnyCallableT], AnyCallableT]:
10151016
"""Put route decorator with PUT `method`
@@ -1075,7 +1076,7 @@ def delete(
10751076
deprecated: bool = False,
10761077
enable_validation: bool | None = None,
10771078
custom_response_validation_http_code: int | HTTPStatus | None = None,
1078-
status_code: int = 200,
1079+
status_code: int = DEFAULT_STATUS_CODE,
10791080
middlewares: list[Callable[..., Any]] | None = None,
10801081
) -> Callable[[AnyCallableT], AnyCallableT]:
10811082
"""Delete route decorator with DELETE `method`
@@ -1140,7 +1141,7 @@ def patch(
11401141
deprecated: bool = False,
11411142
enable_validation: bool | None = None,
11421143
custom_response_validation_http_code: int | HTTPStatus | None = None,
1143-
status_code: int = 200,
1144+
status_code: int = DEFAULT_STATUS_CODE,
11441145
middlewares: list[Callable] | None = None,
11451146
) -> Callable[[AnyCallableT], AnyCallableT]:
11461147
"""Patch route decorator with PATCH `method`
@@ -1208,7 +1209,7 @@ def head(
12081209
deprecated: bool = False,
12091210
enable_validation: bool | None = None,
12101211
custom_response_validation_http_code: int | HTTPStatus | None = None,
1211-
status_code: int = 200,
1212+
status_code: int = DEFAULT_STATUS_CODE,
12121213
middlewares: list[Callable] | None = None,
12131214
) -> Callable[[AnyCallableT], AnyCallableT]:
12141215
"""Head route decorator with HEAD `method`
@@ -2348,7 +2349,7 @@ def route(
23482349
deprecated: bool = False,
23492350
enable_validation: bool | None = None,
23502351
custom_response_validation_http_code: int | HTTPStatus | None = None,
2351-
status_code: int = 200,
2352+
status_code: int = DEFAULT_STATUS_CODE,
23522353
middlewares: list[Callable[..., Any]] | None = None,
23532354
) -> Callable[[AnyCallableT], AnyCallableT]:
23542355
"""Route decorator includes parameter `method`"""
@@ -2899,7 +2900,7 @@ def route(
28992900
deprecated: bool = False,
29002901
enable_validation: bool | None = None,
29012902
custom_response_validation_http_code: int | HTTPStatus | None = None,
2902-
status_code: int = 200,
2903+
status_code: int = DEFAULT_STATUS_CODE,
29032904
middlewares: list[Callable[..., Any]] | None = None,
29042905
) -> Callable[[AnyCallableT], AnyCallableT]:
29052906
def register_route(func: AnyCallableT) -> AnyCallableT:
@@ -2998,7 +2999,7 @@ def route(
29982999
deprecated: bool = False,
29993000
enable_validation: bool | None = None,
30003001
custom_response_validation_http_code: int | HTTPStatus | None = None,
3001-
status_code: int = 200,
3002+
status_code: int = DEFAULT_STATUS_CODE,
30023003
middlewares: list[Callable[..., Any]] | None = None,
30033004
) -> Callable[[AnyCallableT], AnyCallableT]:
30043005
# NOTE: see #1552 for more context.

aws_lambda_powertools/event_handler/bedrock_agent.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
DEFAULT_API_VERSION,
1616
DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
1717
DEFAULT_OPENAPI_VERSION,
18+
DEFAULT_STATUS_CODE,
1819
)
1920

2021
if TYPE_CHECKING:
@@ -129,7 +130,7 @@ def get( # type: ignore[override]
129130
deprecated: bool = False,
130131
enable_validation: bool | None = None,
131132
custom_response_validation_http_code: int | HTTPStatus | None = None,
132-
status_code: int = 200,
133+
status_code: int = DEFAULT_STATUS_CODE,
133134
middlewares: list[Callable[..., Any]] | None = None,
134135
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
135136
security = None
@@ -174,7 +175,7 @@ def post( # type: ignore[override]
174175
deprecated: bool = False,
175176
enable_validation: bool | None = None,
176177
custom_response_validation_http_code: int | HTTPStatus | None = None,
177-
status_code: int = 200,
178+
status_code: int = DEFAULT_STATUS_CODE,
178179
middlewares: list[Callable[..., Any]] | None = None,
179180
):
180181
security = None
@@ -219,7 +220,7 @@ def put( # type: ignore[override]
219220
deprecated: bool = False,
220221
enable_validation: bool | None = None,
221222
custom_response_validation_http_code: int | HTTPStatus | None = None,
222-
status_code: int = 200,
223+
status_code: int = DEFAULT_STATUS_CODE,
223224
middlewares: list[Callable[..., Any]] | None = None,
224225
):
225226
security = None
@@ -264,7 +265,7 @@ def patch( # type: ignore[override]
264265
deprecated: bool = False,
265266
enable_validation: bool | None = None,
266267
custom_response_validation_http_code: int | HTTPStatus | None = None,
267-
status_code: int = 200,
268+
status_code: int = DEFAULT_STATUS_CODE,
268269
middlewares: list[Callable] | None = None,
269270
):
270271
security = None
@@ -309,7 +310,7 @@ def delete( # type: ignore[override]
309310
deprecated: bool = False,
310311
enable_validation: bool | None = None,
311312
custom_response_validation_http_code: int | HTTPStatus | None = None,
312-
status_code: int = 200,
313+
status_code: int = DEFAULT_STATUS_CODE,
313314
middlewares: list[Callable[..., Any]] | None = None,
314315
):
315316
security = None

aws_lambda_powertools/event_handler/openapi/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
DEFAULT_OPENAPI_TITLE = "Powertools for AWS Lambda (Python) API"
44
DEFAULT_CONTENT_TYPE = "application/json"
55
DEFAULT_OPENAPI_RESPONSE_DESCRIPTION = "Successful Response"
6+
DEFAULT_STATUS_CODE = 200

aws_lambda_powertools/event_handler/openapi/schema_generator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from aws_lambda_powertools.event_handler.openapi.constants import (
3737
DEFAULT_CONTENT_TYPE,
3838
DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
39+
DEFAULT_STATUS_CODE,
3940
)
4041

4142

@@ -54,7 +55,7 @@ def generate_openapi_path(
5455
response_description: str | None,
5556
body_field: ModelField | None,
5657
custom_response_validation_http_code: HTTPStatus | None,
57-
status_code: int = 200,
58+
status_code: int = DEFAULT_STATUS_CODE,
5859
dependant: Dependant,
5960
operation_ids: set[str],
6061
model_name_map: dict[TypeModelOrEnum, str],
@@ -222,7 +223,7 @@ def _build_responses(
222223
responses: dict[int, OpenAPIResponse] | None,
223224
response_description: str | None,
224225
custom_response_validation_http_code: HTTPStatus | None,
225-
status_code: int = 200,
226+
status_code: int = DEFAULT_STATUS_CODE,
226227
dependant: Dependant,
227228
model_name_map: dict[TypeModelOrEnum, str],
228229
field_mapping: dict[tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue],

0 commit comments

Comments
 (0)