Skip to content

feat(event-handler): add compress option when serving Swagger HTML #3946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,7 @@ def enable_swagger(
license_info: Optional["License"] = None,
swagger_base_url: Optional[str] = None,
middlewares: Optional[List[Callable[..., Response]]] = None,
compress: bool = False,
):
"""
Returns the OpenAPI schema as a JSON serializable dict
Expand Down Expand Up @@ -1655,11 +1656,13 @@ def enable_swagger(
The base url for the swagger UI. If not provided, we will serve a recent version of the Swagger UI.
middlewares: List[Callable[..., Response]], optional
List of middlewares to be used for the swagger route.
compress: bool, default = False
Whether or not to enable gzip compression swagger route.
"""
from aws_lambda_powertools.event_handler.openapi.compat import model_json
from aws_lambda_powertools.event_handler.openapi.models import Server

@self.get(path, middlewares=middlewares, include_in_schema=False)
@self.get(path, middlewares=middlewares, include_in_schema=False, compress=compress)
def swagger_handler():
base_path = self._get_base_path()

Expand Down
9 changes: 5 additions & 4 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,12 @@ Behind the scenes, the [data validation](#data-validation) feature auto-generate

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

| Caveat | Description |
| Caveat | Description |
| ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 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. |
| **No micro-functions support** yet | Swagger UI is enabled on a per resolver instance which will limit its accuracy here. |
| 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. |
| 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. |
| **No micro-functions support** yet | Swagger UI is enabled on a per resolver instance which will limit its accuracy here. |
| 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. |
| 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. |

```python hl_lines="12-13" title="enabling_swagger.py"
--8<-- "examples/event_handler_rest/src/enabling_swagger.py"
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/event_handler/test_openapi_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def test_openapi_swagger():
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]


def test_openapi_swagger_compressed():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(compress=True)
LOAD_GW_EVENT["headers"] = {"Accept-Encoding": "gzip, deflate, br"}
LOAD_GW_EVENT["path"] = "/swagger"
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["isBase64Encoded"]
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]
assert result["multiValueHeaders"]["Content-Encoding"] == ["gzip"]


def test_openapi_swagger_with_custom_base_url():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(swagger_base_url="https://aws.amazon.com")
Expand Down