Skip to content

Commit 72b1fba

Browse files
authored
fix(event-handler): swagger schema respects api stage (#3796)
1 parent b266979 commit 72b1fba

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-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

+27
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,30 @@ def test_openapi_swagger_json_view_with_custom_path():
7373
assert result["multiValueHeaders"]["Content-Type"] == ["application/json"]
7474
assert isinstance(json.loads(result["body"]), Dict)
7575
assert "OpenAPI JSON View" in result["body"]
76+
77+
78+
def test_openapi_swagger_with_rest_api_default_stage():
79+
app = APIGatewayRestResolver(enable_validation=True)
80+
app.enable_swagger()
81+
82+
event = load_event("apiGatewayProxyEvent.json")
83+
event["path"] = "/swagger"
84+
event["requestContext"]["stage"] = "$default"
85+
86+
result = app(event, {})
87+
assert result["statusCode"] == 200
88+
assert "ui.specActions.updateUrl('/swagger?format=json')" in result["body"]
89+
90+
91+
def test_openapi_swagger_with_rest_api_stage():
92+
app = APIGatewayRestResolver(enable_validation=True)
93+
app.enable_swagger()
94+
95+
event = load_event("apiGatewayProxyEvent.json")
96+
event["path"] = "/swagger"
97+
event["requestContext"]["stage"] = "prod"
98+
event["requestContext"]["path"] = "/prod/swagger"
99+
100+
result = app(event, {})
101+
assert result["statusCode"] == 200
102+
assert "ui.specActions.updateUrl('/prod/swagger?format=json')" in result["body"]

0 commit comments

Comments
 (0)