-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a new crate `utoipa-axum` which provides bindings between `axum` and `utoipa`. It aims to blend as much as possible to the existing philosophy of axum way of registering handlers. This commit introduces new `OpenApiRouter` what wraps `OpenApi` and axum `Router` which provides passthrough implementation for most of the axum Router methods and collects and combines the `OpenApi` from registered routes. Routes registred only via `routes!()` macro will get added to the `OpenApi`. Also this commit introduces `routes!()` macro which collects axum handlers annotated with `#[utoipa::path()]` attribute macro to single paths intance which is then provided to the `OpenApiRouter`. Example of supported sytanx. ```rust let user_router: OpenApiRouter = OpenApiRouter::new() .routes(routes!(search_user)) .routes(routes!(get_user, post_user, delete_user)); ``` Fixes #991
- Loading branch information
Showing
18 changed files
with
808 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "utoipa-axum" | ||
description = "Compile time generated OpenAPI documentation for Rust" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
readme = "README.md" | ||
keywords = ["utoipa", "axum", "bindings"] | ||
repository = "https://github.com/juhaku/utoipa" | ||
categories = ["web-programming"] | ||
authors = ["Juha Kukkonen <[email protected]>"] | ||
rust-version.workspace = true | ||
|
||
[features] | ||
debug = [] | ||
|
||
[dependencies] | ||
axum = { version = "0.7", default-features = false } | ||
utoipa = { version = "5.0.0-alpha", path = "../utoipa", default-features = false } | ||
async-trait = "0.1" | ||
tower-service = "0.3" | ||
tower-layer = "0.3.2" | ||
paste = "1.0" | ||
|
||
[package.metadata.docs.rs] | ||
features = [] | ||
rustdoc-args = ["--cfg", "doc_cfg"] | ||
|
||
[lints.rust] | ||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(doc_cfg)'] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-APACHE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# utoipa-axum - Bindings for Axum and utoipa | ||
|
||
Utoipa axum brings `utoipa` and `axum` closer together by the way of providing an ergonomic API that is extending on | ||
the `axum` API. It gives a natural way to register handlers known to `axum` and also simultaneously generates OpenAPI | ||
specification from the handlers. | ||
|
||
## Install | ||
|
||
Add dependency declaration to `Cargo.toml`. | ||
|
||
```toml | ||
[dependencies] | ||
utoipa_axum = "0.1" | ||
``` | ||
|
||
## Examples | ||
|
||
Use `OpenApiRouter` to collect handlers with `#[utoipa::path]` macro to compose service and form OpenAPI spec. | ||
|
||
```rust | ||
#[derive(utoipa::ToSchema)] | ||
struct Todo { | ||
id: i32, | ||
} | ||
|
||
#[derive(utoipa::OpenApi)] | ||
#[openapi(components(schemas(Todo)))] | ||
struct Api; | ||
|
||
let mut router: OpenApiRouter = OpenApiRouter::with_openapi(Api::openapi()) | ||
.routes(get_path(search_user)) | ||
.routes( | ||
get_path(get_user) | ||
.post_path(post_user) | ||
.delete_path(delete_user), | ||
); | ||
|
||
let api = router.to_openapi(); | ||
let axum_router: axum::Router = router.into(); | ||
``` | ||
|
||
## License | ||
|
||
Licensed under either of [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) license at your option. | ||
|
||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate | ||
by you, shall be dual licensed, without any additional terms or conditions. |
Oops, something went wrong.