Skip to content
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

CA3147 handle non-async Task<ActionResult-derived> ASP.NET MVC controller action methods #2737

Merged
merged 1 commit into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ public override void Initialize(AnalysisContext analysisContext)
|| methodSymbol.IsStatic
|| !methodSymbol.IsPublic()
|| !(methodSymbol.ReturnType.Inherits(actionResultSymbol) // FxCop implementation only looked at ActionResult-derived return types.
|| (methodSymbol.IsAsync
&& wellKnownTypeProvider.IsTaskOfType(
methodSymbol.ReturnType,
(ITypeSymbol typeArgument) => typeArgument.Inherits(actionResultSymbol))))
|| wellKnownTypeProvider.IsTaskOfType(
methodSymbol.ReturnType,
(ITypeSymbol typeArgument) => typeArgument.Inherits(actionResultSymbol)))
|| (!methodSymbol.ContainingType.Inherits(mvcControllerSymbol)
&& !methodSymbol.ContainingType.Inherits(mvcControllerBaseSymbol)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,73 @@ public async Task<ActionResult> DoSomethingAsync(string input)
return null;
}
}
}"
);
}

[Fact]
public async Task MissingVerbsAndTokenTaskButNotAsync_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;

public class ApiController : Controller
{
public Task<ActionResult> DoSomethingAsync(string input)
{
return null;
}
}
}
",
GetCA3147CSharpNoVerbsNoToken(SystemWebMvcNamespaceCSharpLineCount + 9, 35, "DoSomethingAsync"));
}

[Fact]
public async Task AcceptVerbsNoTokenTaskButNotAsync_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;

public class AcceptVerbsNoTokenController : Controller
{
private const HttpVerbs AllowedVerbs = HttpVerbs.Post | HttpVerbs.Put;

[AcceptVerbs(AllowedVerbs)]
public Task<ActionResult> DoSomethingAsync()
{
return null;
}
}
}
",
GetCA3147CSharpVerbsAndNoToken(SystemWebMvcNamespaceCSharpLineCount + 12, 35, "DoSomethingAsync"));
}

[Fact]
public async Task HaveAcceptStringPutAndTokenTaskButNotAsync_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;

public class ApiController : Controller
{
[AcceptVerbs(""Put"")]
[ValidateAntiForgeryToken]
public Task<ActionResult> DoSomethingAsync(string input)
{
return null;
}
}
}"
);
}
Expand Down