Skip to content
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

Update axum to 0.6 RC #345

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/todo-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ authors = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.5"
axum = "0.6.0-rc.2"
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1.17", features = ["full"] }
tower = "0.4"
Expand Down
4 changes: 2 additions & 2 deletions examples/todo-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn main() -> Result<(), Error> {

let store = Arc::new(Store::default());
let app = Router::new()
.merge(SwaggerUi::new("/swagger-ui/*tail").url("/api-doc/openapi.json", ApiDoc::openapi()))
.merge(SwaggerUi::new("/swagger-ui/").url("/api-doc/openapi.json", ApiDoc::openapi()))
.route(
"/todo",
routing::get(todo::list_todos).post(todo::create_todo),
Expand Down Expand Up @@ -173,8 +173,8 @@ mod todo {
)
)]
pub(super) async fn create_todo(
Json(todo): Json<Todo>,
Extension(store): Extension<Arc<Store>>,
Json(todo): Json<Todo>,
) -> impl IntoResponse {
let mut todos = store.lock().await;

Expand Down
2 changes: 1 addition & 1 deletion utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ utoipa = { path = "../utoipa", default-features = false }
serde_json = "1"
serde = "1"
actix-web = { version = "4", features = ["macros"], default-features = false }
axum = "0.5"
axum = "0.6.0-rc.2"
paste = "1"
rocket = "0.5.0-rc.1"
smallvec = { version = "1.9.0", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion utoipa-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ rust-embed = { version = "6.3", features = ["interpolate-folder-path"] }
mime_guess = { version = "2.0" }
actix-web = { version = "4", features = [ "macros" ], optional = true, default-features = false }
rocket = { version = "0.5.0-rc.1", features = ["json"], optional = true }
axum = { version = "0.5", optional = true }
axum = { version = "0.6.0-rc.2", optional = true }
utoipa = { version = "2", path = "../utoipa", default-features = false, features = [] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
Expand Down
2 changes: 1 addition & 1 deletion utoipa-swagger-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Setup Router to serve Swagger UI with **`axum`** framework. See full implementat
Swagger UI with axum from [examples](https://github.com/juhaku/utoipa/tree/master/examples/todo-axum).
```rust
let app = Router::new()
.merge(SwaggerUi::new("/swagger-ui/*tail")
.merge(SwaggerUi::new("/swagger-ui/")
.url("/api-doc/openapi.json", ApiDoc::openapi()));
```

Expand Down
18 changes: 12 additions & 6 deletions utoipa-swagger-ui/src/axum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(feature = "axum")]

use std::borrow::Cow;
use std::sync::Arc;

use axum::{
Expand All @@ -9,15 +10,15 @@ use axum::{

use crate::{Config, SwaggerUi, Url};

impl<B> From<SwaggerUi> for Router<B>
impl<B> From<SwaggerUi> for Router<(), B>
where
B: HttpBody + Send + 'static,
{
fn from(swagger_ui: SwaggerUi) -> Self {
let urls_capacity = swagger_ui.urls.len();

let (router, urls) = swagger_ui.urls.into_iter().fold(
(Router::<B>::new(), Vec::<Url>::with_capacity(urls_capacity)),
(Router::<(), B>::new(), Vec::<Url>::with_capacity(urls_capacity)),
|(router, mut urls), url| {
let (url, openapi) = url;
(
Expand All @@ -38,19 +39,24 @@ where
} else {
Config::new(urls)
};
let config = Arc::new(config);

router.route(
swagger_ui.path.as_ref(),
routing::get(serve_swagger_ui).layer(Extension(Arc::new(config))),
&swagger_ui.path,
routing::get(serve_swagger_ui).layer(Extension(config.clone())),
).route(
&format!("{}*tail", swagger_ui.path),
routing::get(serve_swagger_ui).layer(Extension(config.clone())),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to .clone() here since it is the last usage, thus it can be moved.

Suggested change
routing::get(serve_swagger_ui).layer(Extension(config.clone())),
routing::get(serve_swagger_ui).layer(Extension(config)),

)
}
}

async fn serve_swagger_ui(
Path(tail): Path<String>,
tail: Option<Path<String>>,
Extension(state): Extension<Arc<Config<'static>>>,
) -> impl IntoResponse {
match super::serve(&tail[1..], state) {
let path: Cow<str> = tail.map(|t| t.0.into()).unwrap_or("index.html".into());
match super::serve(&path, state) {
Ok(file) => file
.map(|file| {
(
Expand Down
4 changes: 2 additions & 2 deletions utoipa-swagger-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
//!# where
//!# B: HttpBody + Send + 'static,
//!# {
//! let app = Router::<B>::new()
//! .merge(SwaggerUi::new("/swagger-ui/*tail")
//! let app = Router::<(), B>::new()
//! .merge(SwaggerUi::new("/swagger-ui/")
//! .url("/api-doc/openapi.json", ApiDoc::openapi()));
//!# }
//! ```
Expand Down