From af215e4e80d3fe4e84922214e127443c00edbfb2 Mon Sep 17 00:00:00 2001 From: Himmelschmidt <46351743+Himmelschmidt@users.noreply.github.com> Date: Wed, 5 Nov 2025 12:46:01 -0500 Subject: [PATCH] Fix server functions failing when called from backend code (#4908) * fix(fullstack-macro): allow server functions to be called without HTTP context Server functions can now be invoked directly from server-side code without requiring an HTTP request context. When called outside an HTTP handler, extractors will use default values instead of failing, matching the behavior of the old server_fn implementation. Closes #4907 * fix(fullstack-macro): allow server functions without extractors to be called from backend Functions with no server extractors use Default::default() and work from backend code. Functions with extractors still require HTTP context. Closes #4907 --- packages/fullstack-macro/src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/fullstack-macro/src/lib.rs b/packages/fullstack-macro/src/lib.rs index 325ef7748..835aab1e3 100644 --- a/packages/fullstack-macro/src/lib.rs +++ b/packages/fullstack-macro/src/lib.rs @@ -432,6 +432,15 @@ fn route_impl_with_route( let extracted_as_server_headers = route.extracted_as_server_headers(query_tokens.clone()); + // Generate extraction code based on whether there are server extractors + let extraction_code = if route.server_args.is_empty() { + quote! { Default::default() } + } else { + quote! { + dioxus_fullstack::FullstackContext::extract::<(#(#server_types,)*), _>().await? + } + }; + Ok(quote! { #(#fn_docs)* #route_docs @@ -554,8 +563,12 @@ fn route_impl_with_route( ) } - // Extract the server arguments from the context - let (#(#server_names,)*) = dioxus_fullstack::FullstackContext::extract::<(#(#server_types,)*), _>().await?; + // Extract the server arguments from the context if needed. + // If there are no server extractors, use Default::default() which allows + // the function to be called from backend code without HTTP context. + // If there are server extractors, extraction will fail with a clear error + // when called from backend code without HTTP context. + let (#(#server_names,)*) = #extraction_code; // Call the function directly return #fn_on_server_name #ty_generics(