Skip to content

Commit 946980d

Browse files
xquekquekxleandrodamascena
authored
feat(event-handler): add compress option when serving Swagger HTML (#3946)
* Add compress option to enable_swagger * Adding documentation --------- Co-authored-by: quekx <[email protected]> Co-authored-by: Leandro Damascena <[email protected]>
1 parent c3e36de commit 946980d

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,7 @@ def enable_swagger(
16231623
license_info: Optional["License"] = None,
16241624
swagger_base_url: Optional[str] = None,
16251625
middlewares: Optional[List[Callable[..., Response]]] = None,
1626+
compress: bool = False,
16261627
):
16271628
"""
16281629
Returns the OpenAPI schema as a JSON serializable dict
@@ -1655,11 +1656,13 @@ def enable_swagger(
16551656
The base url for the swagger UI. If not provided, we will serve a recent version of the Swagger UI.
16561657
middlewares: List[Callable[..., Response]], optional
16571658
List of middlewares to be used for the swagger route.
1659+
compress: bool, default = False
1660+
Whether or not to enable gzip compression swagger route.
16581661
"""
16591662
from aws_lambda_powertools.event_handler.openapi.compat import model_json
16601663
from aws_lambda_powertools.event_handler.openapi.models import Server
16611664

1662-
@self.get(path, middlewares=middlewares, include_in_schema=False)
1665+
@self.get(path, middlewares=middlewares, include_in_schema=False, compress=compress)
16631666
def swagger_handler():
16641667
base_path = self._get_base_path()
16651668

docs/core/event_handler/api_gateway.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,12 @@ Behind the scenes, the [data validation](#data-validation) feature auto-generate
524524

525525
There are some important **caveats** that you should know before enabling it:
526526

527-
| Caveat | Description |
527+
| Caveat | Description |
528528
| ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
529-
| 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. |
530-
| **No micro-functions support** yet | Swagger UI is enabled on a per resolver instance which will limit its accuracy here. |
531-
| 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. |
529+
| 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. |
530+
| **No micro-functions support** yet | Swagger UI is enabled on a per resolver instance which will limit its accuracy here. |
531+
| 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. |
532+
| JS and CSS files are **embedded within Swagger HTML** | If you are not using an external CDN to serve Swagger UI assets, we embed JS and CSS directly into the HTML. To enhance performance, please consider enabling the `compress` option to minimize the size of HTTP requests. |
532533

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

tests/functional/event_handler/test_openapi_swagger.py

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def test_openapi_swagger():
1818
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]
1919

2020

21+
def test_openapi_swagger_compressed():
22+
app = APIGatewayRestResolver(enable_validation=True)
23+
app.enable_swagger(compress=True)
24+
LOAD_GW_EVENT["headers"] = {"Accept-Encoding": "gzip, deflate, br"}
25+
LOAD_GW_EVENT["path"] = "/swagger"
26+
result = app(LOAD_GW_EVENT, {})
27+
assert result["statusCode"] == 200
28+
assert result["isBase64Encoded"]
29+
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]
30+
assert result["multiValueHeaders"]["Content-Encoding"] == ["gzip"]
31+
32+
2133
def test_openapi_swagger_with_custom_base_url():
2234
app = APIGatewayRestResolver(enable_validation=True)
2335
app.enable_swagger(swagger_base_url="https://aws.amazon.com")

0 commit comments

Comments
 (0)