-
Notifications
You must be signed in to change notification settings - Fork 228
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
New rule S6934: You should specify the RouteAttribute when an HttpMethodAttribute is specified at an action level #8954
Conversation
1951a32
to
cf8f8bd
Compare
analyzers/tests/SonarAnalyzer.Test/TestCases/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.Test/TestCases/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.Test/TestCases/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
b3a695a
to
5a159d1
Compare
using Microsoft.AspNetCore.Mvc.Routing; | ||
|
||
public partial class HomeController : Controller | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a non-compliant action here. I'm not sure if secondary locations are filtered or not. I'm also not sure if reporting a secondary location in a generated file is a problem or not. But it would be interesting to learn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. These are not filtered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will not change the behavior, I will just document it.
analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
a13fc77
to
8ad9358
Compare
7099130
to
9b498e4
Compare
analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am almost done with the review. I am releasing a bunch of comments, so that they can be addressed while I finish the review. The rule looks very good.
analyzers/tests/SonarAnalyzer.Test/Rules/SpecifyRouteAttributeTest.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.Test/Rules/SpecifyRouteAttributeTest.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs
Outdated
Show resolved
Hide resolved
@costin-zaharia-sonarsource Coverage is almost 100%. I think we should be able to cover |
53d9045
to
5c7cb06
Compare
I have mixed feelings about this. Do you see a specific risk that we take if we don't cover this case? |
5c7cb06
to
1eb7a39
Compare
1eb7a39
to
3042fd3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I have left a few comments, but those are mostly on tests, and there is no need for another round of review. Once addressed or replied to, the PR can be merged.
} | ||
|
||
private static bool CanBeIgnored(string template) => | ||
string.IsNullOrWhiteSpace(template) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very minor concern. I have tried the following out:
public class SomeController : Controller
{
[Route(" ")]
public IActionResult WithRouteAttribute() => View();
}
It registers the route https://localhost:7010/%20
. The same happens with [Route("/t")]
So technically, the rule would apply for any non-null template, including space, tabs, and probably other whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've changed the behavior.
|
||
using MA = Microsoft.AspNetCore; | ||
|
||
public class RouteTemplateIsNotSpecifiedController : Controller |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't seem to have tests for:
- routes with whitespace chars: space and tab in particular
- multiple attributes on the same action
- one attribute with a non-empty route template and one without (non-compliant)
- both attributes without non-empty route templates (compliant)
- both attributes with non-empty route templates (non-compliant)
- mix of
HttpMethodAttribute
andRouteAttribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
public class RouteTemplateIsNotSpecifiedController : Controller | ||
{ | ||
public IActionResult Index() => View(); // Compliant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personal opinion: I would use the name of the method to convey the meaning for the test. For example:
Index
->WithoutHttpAttribute
Index2
->WithHttpAttributeNoRoute
Index3
->WithHttpAttributeAndArgumentListNoRoute
- etc.
This way the intent for the test becomes clearer, and the list is also easy to rearrange, since the increasing numbering doesn't have to be honored.
public IActionResult AbsoluteUri2(string sortBy) => View(); // Compliant, absolute uri | ||
|
||
public IActionResult Error() => View(); // Compliant | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add the following, to document that the "null or whitespace" behavior also applies to Route
:
[Route(" ")]
public IActionResult WithSpaceRouteInRouteAttribute() => View(); // Compliant
[Route("\t")]
public IActionResult WithTabRouteInRouteAttribute() => View(); // Compliant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made them noncompliant, according to: #8954 (comment)
]); | ||
|
||
[TestMethod] | ||
public void SpecifyRouteAttribute_CS() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test should have the _CSharp12
suffix, instead of _CS
.
Quality Gate passedIssues Measures |
Quality Gate passedIssues Measures |
I missed this question. I think it's fine. I tried to do something like the following: class X {
virtual void (string x) { }
} But I got an You can have a missing identifier in a class X {
~() { }
} However, Moreover, the identifier is not optional in the production rule of So I don't see an easy way of generating a case where the Identifier is |
Fixes #8872
Remaining: