diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index 69e1c22c381..7a41c99d053 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -1595,26 +1595,6 @@ def enable_swagger( from aws_lambda_powertools.event_handler.openapi.compat import model_json from aws_lambda_powertools.event_handler.openapi.models import Server - if not swagger_base_url: - - @self.get("/swagger.js", include_in_schema=False) - def swagger_js(): - body = Path.open(Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui-bundle.min.js").read() - return Response( - status_code=200, - content_type="text/javascript", - body=body, - ) - - @self.get("/swagger.css", include_in_schema=False) - def swagger_css(): - body = Path.open(Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui.min.css").read() - return Response( - status_code=200, - content_type="text/css", - body=body, - ) - @self.get(path, middlewares=middlewares, include_in_schema=False) def swagger_handler(): base_path = self._get_base_path() @@ -1623,8 +1603,11 @@ def swagger_handler(): swagger_js = f"{swagger_base_url}/swagger-ui-bundle.min.js" swagger_css = f"{swagger_base_url}/swagger-ui.min.css" else: - swagger_js = f"{base_path}/swagger.js" - swagger_css = f"{base_path}/swagger.css" + # We now inject CSS and JS into the SwaggerUI file + swagger_js = Path.open( + Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui-bundle.min.js", + ).read() + swagger_css = Path.open(Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui.min.css").read() openapi_servers = servers or [Server(url=(base_path or "/"))] @@ -1662,7 +1645,7 @@ def swagger_handler(): body=escaped_spec, ) - body = generate_swagger_html(escaped_spec, path, swagger_js, swagger_css) + body = generate_swagger_html(escaped_spec, path, swagger_js, swagger_css, swagger_base_url) return Response( status_code=200, diff --git a/aws_lambda_powertools/event_handler/openapi/swagger_ui/html.py b/aws_lambda_powertools/event_handler/openapi/swagger_ui/html.py index 7a6b89eb570..fcd644f39f5 100644 --- a/aws_lambda_powertools/event_handler/openapi/swagger_ui/html.py +++ b/aws_lambda_powertools/event_handler/openapi/swagger_ui/html.py @@ -1,4 +1,4 @@ -def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> str: +def generate_swagger_html(spec: str, path: str, swagger_js: str, swagger_css: str, swagger_base_url: str) -> str: """ Generate Swagger UI HTML page @@ -14,6 +14,15 @@ def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> st The URL to the Swagger UI CSS file """ + # If Swagger base URL is present, generate HTML content with linked CSS and JavaScript files + # If no Swagger base URL is provided, include CSS and JavaScript directly in the HTML + if swagger_base_url: + swagger_css_content = f"" + swagger_js_content = f"" + else: + swagger_css_content = f"" + swagger_js_content = f"" + return f""" @@ -24,7 +33,7 @@ def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> st http-equiv="Cache-control" content="no-cache, no-store, must-revalidate" /> - + {swagger_css_content} @@ -33,7 +42,7 @@ def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> st - +{swagger_js_content}