mirror of https://github.com/fafhrd91/actix-web
fix: prevent trailing slashes in scope prefixes
This commit is contained in:
parent
ea06965c4b
commit
ec0fe457de
|
@ -18,19 +18,29 @@ fn with_scope_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenS
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
return Err(syn::Error::new(
|
return Err(syn::Error::new(
|
||||||
Span::call_site(),
|
Span::call_site(),
|
||||||
"missing arguments for scope macro, \
|
"missing arguments for scope macro, expected: #[scope(\"/prefix\")]",
|
||||||
expected: #[scope(\"/prefix\")]",
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let scope_prefix = syn::parse::<syn::LitStr>(args.clone()).map_err(|err| {
|
let scope_prefix = syn::parse::<syn::LitStr>(args.clone()).map_err(|err| {
|
||||||
syn::Error::new(
|
syn::Error::new(
|
||||||
err.span(),
|
err.span(),
|
||||||
"argument to scope macro is not a string literal, \
|
"argument to scope macro is not a string literal, expected: #[scope(\"/prefix\")]",
|
||||||
expected: #[scope(\"/prefix\")]",
|
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
let scope_prefix_value = scope_prefix.value();
|
||||||
|
|
||||||
|
if scope_prefix_value.ends_with("/") {
|
||||||
|
// trailing slashes cause non-obvious problems
|
||||||
|
// it's better to point them out to developers rather than
|
||||||
|
|
||||||
|
return Err(syn::Error::new(
|
||||||
|
scope_prefix.span(),
|
||||||
|
"scopes should not have trailing slashes; see https://docs.rs/actix-web/4/actix_web/struct.Scope.html#avoid-trailing-slashes",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
let mut module = syn::parse::<syn::ItemMod>(input).map_err(|err| {
|
let mut module = syn::parse::<syn::ItemMod>(input).map_err(|err| {
|
||||||
syn::Error::new(err.span(), "#[scope] macro must be attached to a module")
|
syn::Error::new(err.span(), "#[scope] macro must be attached to a module")
|
||||||
})?;
|
})?;
|
||||||
|
@ -43,7 +53,7 @@ fn with_scope_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenS
|
||||||
fun.attrs = fun
|
fun.attrs = fun
|
||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|attr| modify_attribute_with_scope(attr, &scope_prefix.value()))
|
.map(|attr| modify_attribute_with_scope(attr, &scope_prefix_value))
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,7 +62,7 @@ fn with_scope_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenS
|
||||||
Ok(module.to_token_stream().into())
|
Ok(module.to_token_stream().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the attribute is a method type and has a route path, then modify it
|
/// Checks if the attribute is a method type and has a route path, then modifies it.
|
||||||
fn modify_attribute_with_scope(attr: &syn::Attribute, scope_path: &str) -> syn::Attribute {
|
fn modify_attribute_with_scope(attr: &syn::Attribute, scope_path: &str) -> syn::Attribute {
|
||||||
match (attr.parse_args::<RouteArgs>(), attr.clone().meta) {
|
match (attr.parse_args::<RouteArgs>(), attr.clone().meta) {
|
||||||
(Ok(route_args), syn::Meta::List(meta_list)) if has_allowed_methods_in_scope(attr) => {
|
(Ok(route_args), syn::Meta::List(meta_list)) if has_allowed_methods_in_scope(attr) => {
|
||||||
|
|
|
@ -21,6 +21,7 @@ fn compile_macros() {
|
||||||
t.compile_fail("tests/trybuild/scope-on-handler.rs");
|
t.compile_fail("tests/trybuild/scope-on-handler.rs");
|
||||||
t.compile_fail("tests/trybuild/scope-missing-args.rs");
|
t.compile_fail("tests/trybuild/scope-missing-args.rs");
|
||||||
t.compile_fail("tests/trybuild/scope-invalid-args.rs");
|
t.compile_fail("tests/trybuild/scope-invalid-args.rs");
|
||||||
|
t.compile_fail("tests/trybuild/scope-trailing-slash.rs");
|
||||||
|
|
||||||
t.pass("tests/trybuild/docstring-ok.rs");
|
t.pass("tests/trybuild/docstring-ok.rs");
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
use actix_web_codegen::scope;
|
||||||
|
|
||||||
|
#[scope("/api/")]
|
||||||
|
mod api {}
|
||||||
|
|
||||||
|
fn main() {}
|
|
@ -0,0 +1,5 @@
|
||||||
|
error: scopes should not have trailing slashes; see https://docs.rs/actix-web/4/actix_web/struct.Scope.html#avoid-trailing-slashes
|
||||||
|
--> tests/trybuild/scope-trailing-slash.rs:3:9
|
||||||
|
|
|
||||||
|
3 | #[scope("/api/")]
|
||||||
|
| ^^^^^^^
|
Loading…
Reference in New Issue