From 43f8e85c1d7bf8c9d5bff032c834244a21b34c83 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 29 Aug 2023 21:21:48 +0100 Subject: [PATCH] fix: wrap attribute codegen regression when using expression --- actix-web-codegen/CHANGES.md | 2 ++ actix-web-codegen/src/route.rs | 16 ++++++++-------- actix-web-codegen/tests/test_macro.rs | 13 +++++++++++++ .../tests/trybuild/simple-fail.stderr | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index d5a3f5d31..809097e18 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Fix regression when declaring `wrap` attribute using an expression. + ## 4.2.1 - Update `syn` dependency to `2`. diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index 525a1c8ba..6f7cac385 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -224,7 +224,7 @@ struct Args { path: syn::LitStr, resource_name: Option, guards: Vec, - wrappers: Vec, + wrappers: Vec, methods: HashSet, } @@ -251,7 +251,7 @@ impl Args { } else { return Err(syn::Error::new_spanned( nv.value, - "Attribute name expects literal string!", + "Attribute name expects literal string", )); } } else if nv.path.is_ident("guard") { @@ -264,7 +264,7 @@ impl Args { } else { return Err(syn::Error::new_spanned( nv.value, - "Attribute guard expects literal string!", + "Attribute guard expects literal string", )); } } else if nv.path.is_ident("wrap") { @@ -283,9 +283,9 @@ impl Args { } else if nv.path.is_ident("method") { if !is_route_macro { return Err(syn::Error::new_spanned( - &nv, - "HTTP method forbidden here. To handle multiple methods, use `route` instead", - )); + &nv, + "HTTP method forbidden here; to handle multiple methods, use `route` instead.", + )); } else if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(lit), .. @@ -300,13 +300,13 @@ impl Args { } else { return Err(syn::Error::new_spanned( nv.value, - "Attribute method expects literal string!", + "Attribute method expects literal string", )); } } else { return Err(syn::Error::new_spanned( nv.path, - "Unknown attribute key is specified. Allowed: guard, method and wrap", + "Unknown attribute key is specified; allowed: guard, method and wrap", )); } } diff --git a/actix-web-codegen/tests/test_macro.rs b/actix-web-codegen/tests/test_macro.rs index f28654cd9..fb50d4ae0 100644 --- a/actix-web-codegen/tests/test_macro.rs +++ b/actix-web-codegen/tests/test_macro.rs @@ -212,6 +212,19 @@ async fn get_wrap(_: web::Path) -> impl Responder { HttpResponse::Ok() } +/// Using expression, not just path to type, in wrap attribute. +/// +/// Regression from . +#[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] async fn test_params() { let srv = actix_test::start(|| { diff --git a/actix-web-codegen/tests/trybuild/simple-fail.stderr b/actix-web-codegen/tests/trybuild/simple-fail.stderr index 3b3f9d850..284aeab0d 100644 --- a/actix-web-codegen/tests/trybuild/simple-fail.stderr +++ b/actix-web-codegen/tests/trybuild/simple-fail.stderr @@ -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) -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 | 25 | #[delete("/five", method="GET")]