Skip to content

Commit 7a8601b

Browse files
committed
fix(event-handler): swagger schema respects api stage
1 parent b266979 commit 7a8601b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,13 @@ def swagger_handler():
16921692
body=escaped_spec,
16931693
)
16941694

1695-
body = generate_swagger_html(escaped_spec, path, swagger_js, swagger_css, swagger_base_url)
1695+
body = generate_swagger_html(
1696+
escaped_spec,
1697+
f"{base_path}{path}",
1698+
swagger_js,
1699+
swagger_css,
1700+
swagger_base_url,
1701+
)
16961702

16971703
return Response(
16981704
status_code=200,

tests/functional/event_handler/test_openapi_swagger.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from copy import deepcopy
23
from typing import Dict
34

45
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
@@ -73,3 +74,30 @@ def test_openapi_swagger_json_view_with_custom_path():
7374
assert result["multiValueHeaders"]["Content-Type"] == ["application/json"]
7475
assert isinstance(json.loads(result["body"]), Dict)
7576
assert "OpenAPI JSON View" in result["body"]
77+
78+
79+
def test_openapi_swagger_with_rest_api_default_stage():
80+
app = APIGatewayRestResolver(enable_validation=True)
81+
app.enable_swagger()
82+
83+
event = deepcopy(LOAD_GW_EVENT)
84+
event["path"] = "/swagger"
85+
event["requestContext"]["stage"] = "$default"
86+
87+
result = app(event, {})
88+
assert result["statusCode"] == 200
89+
assert "ui.specActions.updateUrl('/swagger?format=json')" in result["body"]
90+
91+
92+
def test_openapi_swagger_with_rest_api_stage():
93+
app = APIGatewayRestResolver(enable_validation=True)
94+
app.enable_swagger()
95+
96+
event = deepcopy(LOAD_GW_EVENT)
97+
event["path"] = "/swagger"
98+
event["requestContext"]["stage"] = "prod"
99+
event["requestContext"]["path"] = "/prod/swagger"
100+
101+
result = app(event, {})
102+
assert result["statusCode"] == 200
103+
assert "ui.specActions.updateUrl('/prod/swagger?format=json')" in result["body"]

0 commit comments

Comments
 (0)