forbid method in non route macros

This commit is contained in:
Arniu 2020-09-22 22:57:31 +08:00
parent 60d21e68d8
commit f393ca28d8
3 changed files with 19 additions and 1 deletions

View File

@ -89,6 +89,8 @@ impl Args {
let mut guards = Vec::new(); let mut guards = Vec::new();
let mut wrappers = Vec::new(); let mut wrappers = Vec::new();
let mut methods = HashSet::new(); let mut methods = HashSet::new();
let is_route_macro = method.is_none();
if let Some(method) = method { if let Some(method) = method {
methods.insert(method); methods.insert(method);
} }
@ -126,7 +128,12 @@ impl Args {
)); ));
} }
} else if nv.path.is_ident("method") { } else if nv.path.is_ident("method") {
if let syn::Lit::Str(ref lit) = nv.lit { if !is_route_macro {
return Err(syn::Error::new_spanned(
&nv,
"HTTP method forbidden here. To handle multiple methods, use `route` instead",
));
} else if let syn::Lit::Str(ref lit) = nv.lit {
let method = MethodType::try_from(lit)?; let method = MethodType::try_from(lit)?;
if !methods.insert(method) { if !methods.insert(method) {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(

View File

@ -22,4 +22,9 @@ async fn four() -> impl Responder {
HttpResponse::Ok() HttpResponse::Ok()
} }
#[delete("/five", method="GET")]
async fn five() -> impl Responder {
HttpResponse::Ok()
}
fn main() {} fn main() {}

View File

@ -21,3 +21,9 @@ error: Multiple paths specified! Should be only one!
| |
20 | #[delete("/four", "/five")] 20 | #[delete("/four", "/five")]
| ^^^^^^^ | ^^^^^^^
error: HTTP method forbidden here. To handle multiple methods, use `route` instead
--> $DIR/simple-fail.rs:25:19
|
25 | #[delete("/five", method="GET")]
| ^^^^^^^^^^^^