Fix runtime error on root route

This commit is contained in:
Tristan D. 2025-03-09 20:17:01 +01:00
parent 11b201004a
commit 53c8b81450
Signed by: tristan
SSH key fingerprint: SHA256:9oFM1J63hYWJjCnLG6C0fxBS15rwNcWwdQNMOHYKJ/4
4 changed files with 25 additions and 16 deletions

View file

@ -12,4 +12,4 @@ keywords = ["axum", "controller", "macro", "routing"]
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
readme = "README.md" readme = "README.md"
repository = "https://git.vlt81.de/vault81/axum-controller" repository = "https://git.vlt81.de/vault81/axum-controller"
version = "0.2.0" version = "0.2.1"

View file

@ -131,6 +131,7 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream {
.map(move |route| { .map(move |route| {
quote! { quote! {
.typed_route(#struct_name :: #route) .typed_route(#struct_name :: #route)
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -139,6 +140,17 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream {
.nest(#route, __nested_router) .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 let middleware_calls = args
.middlewares .middlewares
.clone() .clone()
@ -158,8 +170,7 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream {
.with_state(state) .with_state(state)
; ;
axum::Router::new() #maybe_nesting_call
#nesting_call
} }
} }
}; };

View file

@ -1,6 +1,6 @@
#![allow(unused)] #![allow(unused)]
use axum::extract::{State, Json}; use axum::extract::{Json, State};
use axum_typed_routing::{TypedRouter, route}; use axum_typed_routing::{route, TypedRouter};
#[route(GET "/item/:id?amount&offset")] #[route(GET "/item/:id?amount&offset")]
async fn item_handler( async fn item_handler(

View file

@ -113,7 +113,6 @@ async fn test_wildcard() {
assert_eq!(response.json::<String>(), "foo/bar"); assert_eq!(response.json::<String>(), "foo/bar");
} }
#[cfg(feature = "aide")] #[cfg(feature = "aide")]
mod aide_support { mod aide_support {
use super::*; use super::*;
@ -231,4 +230,3 @@ mod aide_support {
.unwrap() .unwrap()
} }
} }