Skip to content

Commit

Permalink
fix(event-handler): swagger schema respects api stage (#3796)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfonseca authored Feb 19, 2024
1 parent b266979 commit 72b1fba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,13 @@ def swagger_handler():
body=escaped_spec,
)

body = generate_swagger_html(escaped_spec, path, swagger_js, swagger_css, swagger_base_url)
body = generate_swagger_html(
escaped_spec,
f"{base_path}{path}",
swagger_js,
swagger_css,
swagger_base_url,
)

return Response(
status_code=200,
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/event_handler/test_openapi_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,30 @@ def test_openapi_swagger_json_view_with_custom_path():
assert result["multiValueHeaders"]["Content-Type"] == ["application/json"]
assert isinstance(json.loads(result["body"]), Dict)
assert "OpenAPI JSON View" in result["body"]


def test_openapi_swagger_with_rest_api_default_stage():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger()

event = load_event("apiGatewayProxyEvent.json")
event["path"] = "/swagger"
event["requestContext"]["stage"] = "$default"

result = app(event, {})
assert result["statusCode"] == 200
assert "ui.specActions.updateUrl('/swagger?format=json')" in result["body"]


def test_openapi_swagger_with_rest_api_stage():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger()

event = load_event("apiGatewayProxyEvent.json")
event["path"] = "/swagger"
event["requestContext"]["stage"] = "prod"
event["requestContext"]["path"] = "/prod/swagger"

result = app(event, {})
assert result["statusCode"] == 200
assert "ui.specActions.updateUrl('/prod/swagger?format=json')" in result["body"]

0 comments on commit 72b1fba

Please sign in to comment.