Skip to content

Commit be03cc0

Browse files
Merge branch 'develop' into redis/newbackend
2 parents 255a720 + 2acdcc4 commit be03cc0

File tree

7 files changed

+19
-66
lines changed

7 files changed

+19
-66
lines changed

Diff for: aws_lambda_powertools/event_handler/api_gateway.py

+6-23
Original file line numberDiff line numberDiff line change
@@ -1595,26 +1595,6 @@ def enable_swagger(
15951595
from aws_lambda_powertools.event_handler.openapi.compat import model_json
15961596
from aws_lambda_powertools.event_handler.openapi.models import Server
15971597

1598-
if not swagger_base_url:
1599-
1600-
@self.get("/swagger.js", include_in_schema=False)
1601-
def swagger_js():
1602-
body = Path.open(Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui-bundle.min.js").read()
1603-
return Response(
1604-
status_code=200,
1605-
content_type="text/javascript",
1606-
body=body,
1607-
)
1608-
1609-
@self.get("/swagger.css", include_in_schema=False)
1610-
def swagger_css():
1611-
body = Path.open(Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui.min.css").read()
1612-
return Response(
1613-
status_code=200,
1614-
content_type="text/css",
1615-
body=body,
1616-
)
1617-
16181598
@self.get(path, middlewares=middlewares, include_in_schema=False)
16191599
def swagger_handler():
16201600
base_path = self._get_base_path()
@@ -1623,8 +1603,11 @@ def swagger_handler():
16231603
swagger_js = f"{swagger_base_url}/swagger-ui-bundle.min.js"
16241604
swagger_css = f"{swagger_base_url}/swagger-ui.min.css"
16251605
else:
1626-
swagger_js = f"{base_path}/swagger.js"
1627-
swagger_css = f"{base_path}/swagger.css"
1606+
# We now inject CSS and JS into the SwaggerUI file
1607+
swagger_js = Path.open(
1608+
Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui-bundle.min.js",
1609+
).read()
1610+
swagger_css = Path.open(Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui.min.css").read()
16281611

16291612
openapi_servers = servers or [Server(url=(base_path or "/"))]
16301613

@@ -1662,7 +1645,7 @@ def swagger_handler():
16621645
body=escaped_spec,
16631646
)
16641647

1665-
body = generate_swagger_html(escaped_spec, path, swagger_js, swagger_css)
1648+
body = generate_swagger_html(escaped_spec, path, swagger_js, swagger_css, swagger_base_url)
16661649

16671650
return Response(
16681651
status_code=200,

Diff for: aws_lambda_powertools/event_handler/openapi/swagger_ui/html.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> str:
1+
def generate_swagger_html(spec: str, path: str, swagger_js: str, swagger_css: str, swagger_base_url: str) -> str:
22
"""
33
Generate Swagger UI HTML page
44
@@ -14,6 +14,15 @@ def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> st
1414
The URL to the Swagger UI CSS file
1515
"""
1616

17+
# If Swagger base URL is present, generate HTML content with linked CSS and JavaScript files
18+
# If no Swagger base URL is provided, include CSS and JavaScript directly in the HTML
19+
if swagger_base_url:
20+
swagger_css_content = f"<link rel='stylesheet' type='text/css' href='{swagger_css}'>"
21+
swagger_js_content = f"<script src='{swagger_js}'></script>"
22+
else:
23+
swagger_css_content = f"<style>{swagger_css}</style>"
24+
swagger_js_content = f"<script>{swagger_js}</script>"
25+
1726
return f"""
1827
<!DOCTYPE html>
1928
<html>
@@ -24,7 +33,7 @@ def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> st
2433
http-equiv="Cache-control"
2534
content="no-cache, no-store, must-revalidate"
2635
/>
27-
<link rel="stylesheet" type="text/css" href="{css_url}">
36+
{swagger_css_content}
2837
</head>
2938
3039
<body>
@@ -33,7 +42,7 @@ def generate_swagger_html(spec: str, path: str, js_url: str, css_url: str) -> st
3342
</div>
3443
</body>
3544
36-
<script src="{js_url}"></script>
45+
{swagger_js_content}
3746
3847
<script>
3948
var swaggerUIOptions = {{

Diff for: aws_lambda_powertools/event_handler/openapi/swagger_ui/swagger-ui-bundle.min.js

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: aws_lambda_powertools/event_handler/openapi/swagger_ui/swagger-ui.min.css

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs/core/event_handler/api_gateway.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ There are some important **caveats** that you should know before enabling it:
484484
| ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
485485
| Swagger UI is **publicly accessible by default** | When using `enable_swagger` method, you can [protect sensitive API endpoints by implementing a custom middleware](#customizing-swagger-ui) using your preferred authorization mechanism. |
486486
| **No micro-functions support** yet | Swagger UI is enabled on a per resolver instance which will limit its accuracy here. |
487-
| You need to expose **new routes** | You'll need to expose the following paths to Lambda: `/swagger`, `/swagger.css`, `/swagger.js`; ignore if you're routing all paths already. |
487+
| You need to expose a **new route** | You'll need to expose the following path to Lambda: `/swagger`; ignore if you're routing this path already. |
488488

489489
```python hl_lines="12-13" title="enabling_swagger.py"
490490
--8<-- "examples/event_handler_rest/src/enabling_swagger.py"

Diff for: examples/event_handler_rest/sam/template.yaml

-10
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,3 @@ Resources:
6767
# Properties:
6868
# Path: /swagger
6969
# Method: GET
70-
# SwaggerUICSS:
71-
# Type: Api
72-
# Properties:
73-
# Path: /swagger.css
74-
# Method: GET
75-
# SwaggerUIJS:
76-
# Type: Api
77-
# Properties:
78-
# Path: /swagger.js
79-
# Method: GET

Diff for: tests/functional/event_handler/test_openapi_swagger.py

-26
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,6 @@ def test_openapi_swagger():
1717
assert result["statusCode"] == 200
1818
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]
1919

20-
# Using our embedded assets
21-
assert "/swagger.css" in result["body"]
22-
assert "/swagger.js" in result["body"]
23-
24-
25-
def test_openapi_embedded_js():
26-
app = APIGatewayRestResolver(enable_validation=True)
27-
app.enable_swagger()
28-
29-
LOAD_GW_EVENT["path"] = "/swagger.js"
30-
31-
result = app(LOAD_GW_EVENT, {})
32-
assert result["statusCode"] == 200
33-
assert result["multiValueHeaders"]["Content-Type"] == ["text/javascript"]
34-
35-
36-
def test_openapi_embedded_css():
37-
app = APIGatewayRestResolver(enable_validation=True)
38-
app.enable_swagger()
39-
40-
LOAD_GW_EVENT["path"] = "/swagger.css"
41-
42-
result = app(LOAD_GW_EVENT, {})
43-
assert result["statusCode"] == 200
44-
assert result["multiValueHeaders"]["Content-Type"] == ["text/css"]
45-
4620

4721
def test_openapi_swagger_with_custom_base_url():
4822
app = APIGatewayRestResolver(enable_validation=True)

0 commit comments

Comments
 (0)