diff --git a/examples/todo-axum/Cargo.toml b/examples/todo-axum/Cargo.toml index 57591350..a1c432e2 100644 --- a/examples/todo-axum/Cargo.toml +++ b/examples/todo-axum/Cargo.toml @@ -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" diff --git a/examples/todo-axum/src/main.rs b/examples/todo-axum/src/main.rs index 17072fe9..f0686939 100644 --- a/examples/todo-axum/src/main.rs +++ b/examples/todo-axum/src/main.rs @@ -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), @@ -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; diff --git a/utoipa-gen/Cargo.toml b/utoipa-gen/Cargo.toml index 8b87b072..a972c666 100644 --- a/utoipa-gen/Cargo.toml +++ b/utoipa-gen/Cargo.toml @@ -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"] } diff --git a/utoipa-swagger-ui/Cargo.toml b/utoipa-swagger-ui/Cargo.toml index a46e4880..0c616d8b 100644 --- a/utoipa-swagger-ui/Cargo.toml +++ b/utoipa-swagger-ui/Cargo.toml @@ -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" } diff --git a/utoipa-swagger-ui/README.md b/utoipa-swagger-ui/README.md index b757aaa3..f611e239 100644 --- a/utoipa-swagger-ui/README.md +++ b/utoipa-swagger-ui/README.md @@ -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())); ``` diff --git a/utoipa-swagger-ui/src/axum.rs b/utoipa-swagger-ui/src/axum.rs index c99e2e95..082692fb 100644 --- a/utoipa-swagger-ui/src/axum.rs +++ b/utoipa-swagger-ui/src/axum.rs @@ -1,5 +1,6 @@ #![cfg(feature = "axum")] +use std::borrow::Cow; use std::sync::Arc; use axum::{ @@ -9,7 +10,7 @@ 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, { @@ -17,7 +18,7 @@ where 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; ( @@ -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())), ) } } 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| { ( diff --git a/utoipa-swagger-ui/src/lib.rs b/utoipa-swagger-ui/src/lib.rs index 6ac3b2fe..841102cb 100644 --- a/utoipa-swagger-ui/src/lib.rs +++ b/utoipa-swagger-ui/src/lib.rs @@ -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())); //!# } //! ```