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

Fix codegen issue for parameters with lifetimes #1839

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions core/codegen/src/attribute/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,39 @@ fn query_decls(route: &Route) -> Option<TokenStream> {

#[allow(non_snake_case)]
Some(quote! {
let mut __e = #_form::Errors::new();
#(let mut #ident = #init_expr;)*

for _f in #__req.query_fields() {
let _raw = (_f.name.source().as_str(), _f.value);
let _key = _f.name.key_lossy().as_str();
match (_raw, _key) {
// Skip static parameters so <param..> doesn't see them.
#(((#raw_name, #raw_value), _) => { /* skip */ },)*
#((_, #matcher) => #push_expr,)*
_ => { /* in case we have no trailing, ignore all else */ },
let (#(#ident),*) = {
let mut __e = #_form::Errors::new();
#(let mut #ident = #init_expr;)*

for _f in #__req.query_fields() {
let _raw = (_f.name.source().as_str(), _f.value);
let _key = _f.name.key_lossy().as_str();
match (_raw, _key) {
// Skip static parameters so <param..> doesn't see them.
#(((#raw_name, #raw_value), _) => { /* skip */ },)*
#((_, #matcher) => #push_expr,)*
_ => { /* in case we have no trailing, ignore all else */ },
}
}
}

#(
let #ident = match #finalize_expr {
#_Ok(_v) => #_Some(_v),
#_Err(_err) => {
__e.extend(_err.with_name(#_form::NameView::new(#name)));
#_None
},
};
)*

if !__e.is_empty() {
#_log::warn_!("Query string failed to match route declaration.");
for _err in __e { #_log::warn_!("{}", _err); }
return #Outcome::Forward(#__data);
}
#(
let #ident = match #finalize_expr {
#_Ok(_v) => #_Some(_v),
#_Err(_err) => {
__e.extend(_err.with_name(#_form::NameView::new(#name)));
#_None
},
};
)*

if !__e.is_empty() {
#_log::warn_!("Query string failed to match route declaration.");
for _err in __e { #_log::warn_!("{}", _err); }
return #Outcome::Forward(#__data);
}

#(let #ident = #ident.unwrap();)*
(#(#ident.unwrap()),*)
};
})
}

Expand Down
6 changes: 6 additions & 0 deletions core/codegen/tests/async-routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ async fn hello(_origin: &Origin<'_>) -> &'static str {
"Hello, world!"
}

#[get("/repeated_query?<sort>")]
async fn repeated_query(sort: Vec<&str>) -> &str {
noop().await;
sort[0]
}

#[catch(404)]
async fn not_found(req: &Request<'_>) -> String {
noop().await;
Expand Down