diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index 43b5bf139ea..271c767c060 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -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, diff --git a/tests/functional/event_handler/test_openapi_swagger.py b/tests/functional/event_handler/test_openapi_swagger.py index 27fca16f2fa..45e908742b4 100644 --- a/tests/functional/event_handler/test_openapi_swagger.py +++ b/tests/functional/event_handler/test_openapi_swagger.py @@ -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"]