Skip to content

Commit

Permalink
docs: SecurityAddon
Browse files Browse the repository at this point in the history
  • Loading branch information
NexVeridian committed Dec 14, 2024
1 parent e1a64a6 commit 85cf36a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
23 changes: 20 additions & 3 deletions docs-site/content/docs/the-app/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ server:
# spec_yaml_url: /api-docs/openapi.yaml
```
## Inital OpenAPI Spec
Modifies the OpenAPI spec before the routes are added, allowing you to edit [openapi::info](https://docs.rs/utoipa/latest/utoipa/openapi/info/struct.Info.html)
Modifies the OpenAPI spec before the routes are added, allowing you to edit [`openapi::info`](https://docs.rs/utoipa/latest/utoipa/openapi/info/struct.Info.html)

```rust
// src/app.rs
Expand All @@ -823,7 +823,7 @@ impl Hooks for App {
```

## Generating the OpenAPI spec for a route
Only routes that are annotated with `utoipa::path` will be included in the OpenAPI spec.
Only routes that are annotated with [`utoipa::path`](https://docs.rs/utoipa/latest/utoipa/attr.path.html) will be included in the OpenAPI spec.

```rust
#[utoipa::path(
Expand All @@ -841,7 +841,7 @@ async fn get_action_openapi() -> Result<Response> {
}
```

Make sure to add `#[derive(ToSchema)]` on any struct that included in `utoipa::path`.
Make sure to add `#[derive(ToSchema)]` on any struct that included in [`utoipa::path`](https://docs.rs/utoipa/latest/utoipa/attr.path.html).
```rust
use utoipa::ToSchema;

Expand All @@ -852,6 +852,23 @@ pub struct Album {
}
```

If `modifiers(&SecurityAddon)` is set in `inital_openapi_spec`, you can document the per route security in `utoipa::path`:
- `security(("jwt_token" = []))`
- `security(("api_key" = []))`
- or leave blank to remove security from the route `security()`

Example:
```rust
#[utoipa::path(
get,
path = "/album",
security(("jwt_token" = [])),
responses(
(status = 200, description = "Album found", body = Album),
),
)]
```

## Adding routes to the OpenAPI spec visualizer
Swap the `axum::routing::MethodFilter` to `routes!`
### Before
Expand Down
1 change: 1 addition & 0 deletions tests/infra_cfg/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct Album {
#[utoipa::path(
get,
path = "/album",
security(("jwt_token" = [])),
responses(
(status = 200, description = "Album found", body = Album),
),
Expand Down

0 comments on commit 85cf36a

Please sign in to comment.