Skip to content

Commit 5374efb

Browse files
docs: add openapi docs (#7939)
* docs: add openapi docs * Addressing Ran's feedback --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ae16510 commit 5374efb

20 files changed

+841
-341
lines changed

aws_lambda_powertools/event_handler/openapi/merge.py

Lines changed: 187 additions & 196 deletions
Large diffs are not rendered by default.

docs/core/event_handler/api_gateway.md

Lines changed: 8 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ This value will override the value of the failed response validation http code s
485485

486486
We use the `Annotated` type to tell the Event Handler that a particular parameter is not only an optional string, but also a query string with constraints.
487487

488-
In the following example, we use a new `Query` OpenAPI type to add [one out of many possible constraints](#customizing-openapi-parameters), which should read as:
488+
In the following example, we use a new `Query` OpenAPI type to add [one out of many possible constraints](openapi.md#customizing-parameters), which should read as:
489489

490490
* `completed` is a query string with a `None` as its default value
491491
* `completed`, when set, should have at minimum 4 characters
@@ -539,7 +539,7 @@ In the following example, we use a new `Query` OpenAPI type to add [one out of m
539539

540540
#### Validating path parameters
541541

542-
Just like we learned in [query string validation](#validating-query-strings), we can use a new `Path` OpenAPI type to [add constraints](#customizing-openapi-parameters).
542+
Just like we learned in [query string validation](#validating-query-strings), we can use a new `Path` OpenAPI type to [add constraints](openapi.md#customizing-parameters).
543543

544544
For example, we could validate that `<todo_id>` dynamic path should be no greater than three digits.
545545

@@ -555,7 +555,7 @@ For example, we could validate that `<todo_id>` dynamic path should be no greate
555555

556556
We use the `Annotated` type to tell the Event Handler that a particular parameter is a header that needs to be validated. Also, we adhere to [HTTP RFC standards](https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2){target="_blank" rel="nofollow"}, which means we treat HTTP headers as case-insensitive.
557557

558-
In the following example, we use a new `Header` OpenAPI type to add [one out of many possible constraints](#customizing-openapi-parameters), which should read as:
558+
In the following example, we use a new `Header` OpenAPI type to add [one out of many possible constraints](openapi.md#customizing-parameters), which should read as:
559559

560560
* `correlation_id` is a header that must be present in the request
561561
* `correlation_id` should have 16 characters
@@ -767,28 +767,15 @@ We provide pre-defined errors for the most popular ones based on [AWS Lambda API
767767

768768
### Enabling SwaggerUI
769769

770-
!!! note "This feature requires [data validation](#data-validation) feature to be enabled."
770+
???+ tip "OpenAPI documentation has moved"
771+
For complete OpenAPI documentation including Swagger UI customization, security schemes, and OpenAPI Merge for micro-functions, see the dedicated [OpenAPI documentation](openapi.md).
771772

772-
Behind the scenes, the [data validation](#data-validation) feature auto-generates an OpenAPI specification from your routes and type annotations. You can use [Swagger UI](https://swagger.io/tools/swagger-ui/){target="_blank" rel="nofollow"} to visualize and interact with your newly auto-documented API.
773-
774-
There are some important **caveats** that you should know before enabling it:
775-
776-
| Caveat | Description |
777-
| ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
778-
| 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. |
779-
| **No micro-functions support** yet | Swagger UI is enabled on a per resolver instance which will limit its accuracy here. |
780-
| 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. |
781-
| 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. |
782-
| Authorization data is **lost** on browser close/refresh | Use `enable_swagger(persist_authorization=True)` to persist authorization data, like OAuath 2.0 access tokens. |
773+
Use `enable_swagger()` to serve interactive API documentation:
783774

784775
```python hl_lines="12-13" title="enabling_swagger.py"
785776
--8<-- "examples/event_handler_rest/src/enabling_swagger.py"
786777
```
787778

788-
1. `enable_swagger` creates a route to serve Swagger UI and allows quick customizations. <br><br> You can also include middlewares to protect or enhance the overall experience.
789-
790-
Here's an example of what it looks like by default:
791-
792779
![Swagger UI picture](../../media/swagger.png)
793780

794781
### Custom Domain API Mappings
@@ -1274,128 +1261,8 @@ This will enable full tracebacks errors in the response, print request and respo
12741261

12751262
### OpenAPI
12761263

1277-
When you enable [Data Validation](#data-validation), we use a combination of Pydantic Models and [OpenAPI](https://www.openapis.org/){target="_blank" rel="nofollow"} type annotations to add constraints to your API's parameters.
1278-
1279-
???+ warning "OpenAPI schema version depends on the installed version of Pydantic"
1280-
Pydantic v1 generates [valid OpenAPI 3.0.3 schemas](https://docs.pydantic.dev/1.10/usage/schema/){target="_blank" rel="nofollow"}, and Pydantic v2 generates [valid OpenAPI 3.1.0 schemas](https://docs.pydantic.dev/latest/why/#json-schema){target="_blank" rel="nofollow"}.
1281-
1282-
In OpenAPI documentation tools like [SwaggerUI](#enabling-swaggerui), these annotations become readable descriptions, offering a self-explanatory API interface. This reduces boilerplate code while improving functionality and enabling auto-documentation.
1283-
1284-
???+ note
1285-
We don't have support for files, form data, and header parameters at the moment. If you're interested in this, please [open an issue](https://github.com/aws-powertools/powertools-lambda-python/issues/new?assignees=&labels=feature-request%2Ctriage&projects=&template=feature_request.yml&title=Feature+request%3A+TITLE).
1286-
1287-
#### Customizing OpenAPI parameters
1288-
1289-
--8<-- "docs/core/event_handler/_openapi_customization_parameters.md"
1290-
1291-
#### Customizing API operations
1292-
1293-
--8<-- "docs/core/event_handler/_openapi_customization_operations.md"
1294-
1295-
To implement these customizations, include extra parameters when defining your routes:
1296-
1297-
```python hl_lines="11-20" title="customizing_api_operations.py"
1298-
--8<-- "examples/event_handler_rest/src/customizing_api_operations.py"
1299-
```
1300-
1301-
#### Customizing OpenAPI metadata
1302-
1303-
--8<-- "docs/core/event_handler/_openapi_customization_metadata.md"
1304-
1305-
Include extra parameters when exporting your OpenAPI specification to apply these customizations:
1306-
1307-
=== "customizing_api_metadata.py"
1308-
1309-
```python hl_lines="8-16"
1310-
--8<-- "examples/event_handler_rest/src/customizing_api_metadata.py"
1311-
```
1312-
1313-
#### Customizing Swagger UI
1314-
1315-
???+note "Customizing the Swagger metadata"
1316-
The `enable_swagger` method accepts the same metadata as described at [Customizing OpenAPI metadata](#customizing-openapi-metadata).
1317-
1318-
The Swagger UI appears by default at the `/swagger` path, but you can customize this to serve the documentation from another path and specify the source for Swagger UI assets.
1319-
1320-
Below is an example configuration for serving Swagger UI from a custom path or CDN, with assets like CSS and JavaScript loading from a chosen CDN base URL.
1321-
1322-
=== "customizing_swagger.py"
1323-
1324-
```python hl_lines="10"
1325-
--8<-- "examples/event_handler_rest/src/customizing_swagger.py"
1326-
```
1327-
1328-
=== "customizing_swagger_middlewares.py"
1329-
1330-
A Middleware can handle tasks such as adding security headers, user authentication, or other request processing for serving the Swagger UI.
1331-
1332-
```python hl_lines="7 13-18 21"
1333-
--8<-- "examples/event_handler_rest/src/customizing_swagger_middlewares.py"
1334-
```
1335-
1336-
#### Security schemes
1337-
1338-
???-info "Does Powertools implement any of the security schemes?"
1339-
No. Powertools adds support for generating OpenAPI documentation with [security schemes](https://swagger.io/docs/specification/authentication/), but it doesn't implement any of the security schemes itself, so you must implement the security mechanisms separately.
1340-
1341-
Security schemes are declared at the top-level first. You can reference them globally or on a per path _(operation)_ level. **However**, if you reference security schemes that are not defined at the top-level it will lead to a `SchemaValidationError` _(invalid OpenAPI spec)_.
1342-
1343-
=== "Global OpenAPI security schemes"
1344-
1345-
```python title="security_schemes_global.py" hl_lines="17-27"
1346-
--8<-- "examples/event_handler_rest/src/security_schemes_global.py"
1347-
```
1348-
1349-
1. Using the oauth security scheme defined earlier, scoped to the "admin" role.
1350-
1351-
=== "Per Operation security"
1352-
1353-
```python title="security_schemes_per_operation.py" hl_lines="17-26 30"
1354-
--8<-- "examples/event_handler_rest/src/security_schemes_per_operation.py"
1355-
```
1356-
1357-
1. Using the oauth security scheme defined bellow, scoped to the "admin" role.
1358-
1359-
=== "Global security schemes and optional security per route"
1360-
1361-
```python title="security_schemes_global_and_optional.py" hl_lines="17-26 35"
1362-
--8<-- "examples/event_handler_rest/src/security_schemes_global_and_optional.py"
1363-
```
1364-
1365-
1. To make security optional in a specific route, an empty security requirement ({}) can be included in the array.
1366-
1367-
OpenAPI 3 lets you describe APIs protected using the following security schemes:
1368-
1369-
| Security Scheme | Type | Description |
1370-
| --------------------------------------------------------------------------------------------------------------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1371-
| [HTTP auth](https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml){target="_blank"} | `HTTPBase` | HTTP authentication schemes using the Authorization header (e.g: [Basic auth](https://swagger.io/docs/specification/authentication/basic-authentication/){target="_blank"}, [Bearer](https://swagger.io/docs/specification/authentication/bearer-authentication/){target="_blank"}) |
1372-
| [API keys](https://swagger.io/docs/specification/authentication/api-keys/){target="_blank"} (e.g: query strings, cookies) | `APIKey` | API keys in headers, query strings or [cookies](https://swagger.io/docs/specification/authentication/cookie-authentication/){target="_blank"}. |
1373-
| [OAuth 2](https://swagger.io/docs/specification/authentication/oauth2/){target="_blank"} | `OAuth2` | Authorization protocol that gives an API client limited access to user data on a web server. |
1374-
| [OpenID Connect Discovery](https://swagger.io/docs/specification/authentication/openid-connect-discovery/){target="_blank"} | `OpenIdConnect` | Identity layer built [on top of the OAuth 2.0 protocol](https://openid.net/developers/how-connect-works/){target="_blank"} and supported by some OAuth 2.0. |
1375-
| [Mutual TLS](https://swagger.io/specification/#security-scheme-object){target="_blank"}. | `MutualTLS` | Client/server certificate mutual authentication scheme. |
1376-
1377-
???-note "Using OAuth2 with the Swagger UI?"
1378-
You can use the `OAuth2Config` option to configure a default OAuth2 app on the generated Swagger UI.
1379-
1380-
```python hl_lines="10 15-18 22"
1381-
--8<-- "examples/event_handler_rest/src/swagger_with_oauth2.py"
1382-
```
1383-
1384-
#### OpenAPI extensions
1385-
1386-
For a better experience when working with Lambda and Amazon API Gateway, customers can define extensions using the `openapi_extensions` parameter. We support defining OpenAPI extensions at the following levels of the OpenAPI JSON Schema: Root, Servers, Operation, and Security Schemes.
1387-
1388-
???+ warning
1389-
We do not support the `x-amazon-apigateway-any-method` and `x-amazon-apigateway-integrations` extensions.
1390-
1391-
```python hl_lines="9 15 25 28" title="Adding OpenAPI extensions"
1392-
--8<-- "examples/event_handler_rest/src/working_with_openapi_extensions.py"
1393-
```
1394-
1395-
1. Server level
1396-
2. Operation level
1397-
3. Security scheme level
1398-
4. Root level
1264+
???+ tip "OpenAPI documentation has moved"
1265+
For complete OpenAPI documentation including customization, security schemes, extensions, and OpenAPI Merge for micro-functions, see the dedicated [OpenAPI documentation](openapi.md).
13991266

14001267
### Custom serializer
14011268

0 commit comments

Comments
 (0)