fix: wrap attribute codegen regression when using expression

This commit is contained in:
Rob Ede 2023-08-29 21:21:48 +01:00
parent 39abe3ae5e
commit 43f8e85c1d
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 24 additions and 9 deletions

View File

@ -2,6 +2,8 @@
## Unreleased ## Unreleased
- Fix regression when declaring `wrap` attribute using an expression.
## 4.2.1 ## 4.2.1
- Update `syn` dependency to `2`. - Update `syn` dependency to `2`.

View File

@ -224,7 +224,7 @@ struct Args {
path: syn::LitStr, path: syn::LitStr,
resource_name: Option<syn::LitStr>, resource_name: Option<syn::LitStr>,
guards: Vec<Path>, guards: Vec<Path>,
wrappers: Vec<syn::Type>, wrappers: Vec<syn::Expr>,
methods: HashSet<MethodTypeExt>, methods: HashSet<MethodTypeExt>,
} }
@ -251,7 +251,7 @@ impl Args {
} else { } else {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
nv.value, nv.value,
"Attribute name expects literal string!", "Attribute name expects literal string",
)); ));
} }
} else if nv.path.is_ident("guard") { } else if nv.path.is_ident("guard") {
@ -264,7 +264,7 @@ impl Args {
} else { } else {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
nv.value, nv.value,
"Attribute guard expects literal string!", "Attribute guard expects literal string",
)); ));
} }
} else if nv.path.is_ident("wrap") { } else if nv.path.is_ident("wrap") {
@ -283,9 +283,9 @@ impl Args {
} else if nv.path.is_ident("method") { } else if nv.path.is_ident("method") {
if !is_route_macro { if !is_route_macro {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
&nv, &nv,
"HTTP method forbidden here. To handle multiple methods, use `route` instead", "HTTP method forbidden here; to handle multiple methods, use `route` instead.",
)); ));
} else if let syn::Expr::Lit(syn::ExprLit { } else if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit), lit: syn::Lit::Str(lit),
.. ..
@ -300,13 +300,13 @@ impl Args {
} else { } else {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
nv.value, nv.value,
"Attribute method expects literal string!", "Attribute method expects literal string",
)); ));
} }
} else { } else {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
nv.path, nv.path,
"Unknown attribute key is specified. Allowed: guard, method and wrap", "Unknown attribute key is specified; allowed: guard, method and wrap",
)); ));
} }
} }

View File

@ -212,6 +212,19 @@ async fn get_wrap(_: web::Path<String>) -> impl Responder {
HttpResponse::Ok() HttpResponse::Ok()
} }
/// Using expression, not just path to type, in wrap attribute.
///
/// Regression from <https://github.com/actix/actix-web/issues/3118>.
#[route(
"/catalog",
method = "GET",
method = "HEAD",
wrap = "actix_web::middleware::Compress::default()"
)]
async fn get_catalog() -> impl Responder {
HttpResponse::Ok().body("123123123")
}
#[actix_rt::test] #[actix_rt::test]
async fn test_params() { async fn test_params() {
let srv = actix_test::start(|| { let srv = actix_test::start(|| {

View File

@ -38,7 +38,7 @@ error: Multiple paths specified! There should be only one.
| |
= note: this error originates in the attribute macro `delete` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the attribute macro `delete` (in Nightly builds, run with -Z macro-backtrace for more info)
error: HTTP method forbidden here. To handle multiple methods, use `route` instead error: HTTP method forbidden here; to handle multiple methods, use `route` instead.
--> $DIR/simple-fail.rs:25:19 --> $DIR/simple-fail.rs:25:19
| |
25 | #[delete("/five", method="GET")] 25 | #[delete("/five", method="GET")]