Skip to content

Commit 767d709

Browse files
authored
[release/7.0-rc1] Fix resolving method declaration in separate file (#43323)
* Fix resolving method declaration in separate file * Update RouteHandlerAnalyzer.cs
1 parent 21c8bec commit 767d709

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteHandlers/RouteHandlerAnalyzer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ public override void Initialize(AnalysisContext context)
7777
var foundMethodReferenceBody = false;
7878
if (!methodReference.Method.DeclaringSyntaxReferences.IsEmpty)
7979
{
80-
var syntaxReference = methodReference.Method.DeclaringSyntaxReferences[0];
81-
var methodOperation = invocation.SemanticModel.GetOperation(syntaxReference.GetSyntax(context.CancellationToken));
80+
var syntaxReference = methodReference.Method.DeclaringSyntaxReferences.Single();
81+
var syntaxNode = syntaxReference.GetSyntax(context.CancellationToken);
82+
var methodOperation = syntaxNode.SyntaxTree == invocation.SemanticModel.SyntaxTree
83+
? invocation.SemanticModel.GetOperation(syntaxNode, context.CancellationToken)
84+
: null;
8285
if (methodOperation is ILocalFunctionOperation { Body: not null } localFunction)
8386
{
8487
foundMethodReferenceBody = true;

src/Framework/AspNetCoreAnalyzers/test/RouteHandlers/DisallowReturningActionResultsFromMapMethodsTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,43 @@ public record Person(string Name);
252252
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location);
253253
Assert.Equal("IActionResult instances should not be returned from a MapPost Delegate parameter. Consider returning an equivalent result from Microsoft.AspNetCore.Http.Results.", diagnostic.GetMessage(CultureInfo.InvariantCulture));
254254
}
255+
256+
[Fact]
257+
public async Task MinimalAction_ReturningActionResultOfTDeclarationInDifferentFile_ProducesDiagnostics()
258+
{
259+
// Arrange
260+
var source = TestSource.Read(@"
261+
using Microsoft.AspNetCore.Builder;
262+
using Microsoft.AspNetCore.Http;
263+
264+
var webApp = WebApplication.Create();
265+
webApp.MapPost(""/"", /*MM*/MyController.ActionResultMethod);
266+
");
267+
var controllerSource = @"
268+
using System.Threading.Tasks;
269+
using Microsoft.AspNetCore.Mvc;
270+
271+
public static class MyController
272+
{
273+
public static async Task<ActionResult<Person>> ActionResultMethod(int id)
274+
{
275+
await Task.Yield();
276+
if (id == 0) return new NotFoundResult();
277+
return new Person(""test"");
278+
}
279+
280+
public record Person(string Name);
281+
}
282+
";
283+
284+
// Act
285+
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source, controllerSource);
286+
287+
// Assert
288+
var diagnostic = Assert.Single(diagnostics);
289+
Assert.Same(DiagnosticDescriptors.DoNotReturnActionResultsFromRouteHandlers, diagnostic.Descriptor);
290+
AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location);
291+
Assert.Equal("IActionResult instances should not be returned from a MapPost Delegate parameter. Consider returning an equivalent result from Microsoft.AspNetCore.Http.Results.", diagnostic.GetMessage(CultureInfo.InvariantCulture));
292+
}
255293
}
256294

0 commit comments

Comments
 (0)