From 53c8b81450f35dffa710ec98672d500cce25dc13 Mon Sep 17 00:00:00 2001 From: Tristan Druyen Date: Sun, 9 Mar 2025 20:17:01 +0100 Subject: [PATCH] Fix runtime error on root route --- Cargo.toml | 2 +- axum-controller-macros/src/lib.rs | 19 +++++++++++++++---- vendor/axum-typed-routing/examples/basic.rs | 18 +++++++++--------- vendor/axum-typed-routing/tests/main.rs | 2 -- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d6b9bd2..2e673ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ keywords = ["axum", "controller", "macro", "routing"] license = "AGPL-3.0-or-later" readme = "README.md" repository = "https://git.vlt81.de/vault81/axum-controller" -version = "0.2.0" +version = "0.2.1" diff --git a/axum-controller-macros/src/lib.rs b/axum-controller-macros/src/lib.rs index 8ef3719..d5d8b6d 100644 --- a/axum-controller-macros/src/lib.rs +++ b/axum-controller-macros/src/lib.rs @@ -130,8 +130,9 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream { .into_iter() .map(move |route| { quote! { - .typed_route(#struct_name :: #route) - } + .typed_route(#struct_name :: #route) + + } }) .collect::>(); @@ -139,6 +140,17 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream { .nest(#route, __nested_router) }; + let maybe_nesting_call = if route == syn::parse_quote!("/") { + quote! { + __nested_router + } + } else { + quote! { + axum::Router::new() + #nesting_call + } + }; + let middleware_calls = args .middlewares .clone() @@ -158,8 +170,7 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream { .with_state(state) ; - axum::Router::new() - #nesting_call + #maybe_nesting_call } } }; diff --git a/vendor/axum-typed-routing/examples/basic.rs b/vendor/axum-typed-routing/examples/basic.rs index d86ba11..2a2332a 100644 --- a/vendor/axum-typed-routing/examples/basic.rs +++ b/vendor/axum-typed-routing/examples/basic.rs @@ -1,20 +1,20 @@ #![allow(unused)] -use axum::extract::{State, Json}; -use axum_typed_routing::{TypedRouter, route}; +use axum::extract::{Json, State}; +use axum_typed_routing::{route, TypedRouter}; #[route(GET "/item/:id?amount&offset")] async fn item_handler( - id: u32, - amount: Option, - offset: Option, - State(state): State, - Json(json): Json, + id: u32, + amount: Option, + offset: Option, + State(state): State, + Json(json): Json, ) -> String { - todo!("handle request") + todo!("handle request") } fn main() { let router: axum::Router = axum::Router::new() .typed_route(item_handler) .with_state("state".to_string()); -} \ No newline at end of file +} diff --git a/vendor/axum-typed-routing/tests/main.rs b/vendor/axum-typed-routing/tests/main.rs index a4680f7..55d2cc5 100644 --- a/vendor/axum-typed-routing/tests/main.rs +++ b/vendor/axum-typed-routing/tests/main.rs @@ -113,7 +113,6 @@ async fn test_wildcard() { assert_eq!(response.json::(), "foo/bar"); } - #[cfg(feature = "aide")] mod aide_support { use super::*; @@ -231,4 +230,3 @@ mod aide_support { .unwrap() } } -