Skip to content

Commit

Permalink
Fix request delegate analyzer error on null return type (dotnet#44534)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Oct 14, 2022
1 parent 4faa84c commit 0cfd468
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public override void Initialize(AnalysisContext context)
var resolvedOperation = WalkDownConversion(returnedValue);
var returnType = resolvedOperation.Type;

if (SymbolEqualityComparer.Default.Equals(returnType.OriginalDefinition, wellKnownTypes.TaskOfT))
// Return type could be null if:
// 1. The method returns null.
// 2. The method throws an exception.
if (returnType != null && SymbolEqualityComparer.Default.Equals(returnType.OriginalDefinition, wellKnownTypes.TaskOfT))
{
AddDiagnosticWarning(context, anonymousFunction.Syntax.GetLocation(), returnType);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,42 @@ namespace Microsoft.AspNetCore.Analyzers.Http;

public class RequestDelegateReturnTypeAnalyzerTests
{
[Fact]
public async Task AnonymousDelegate_RequestDelegate_ThrowError_NoDiagnostics()
{
// Arrange & Act & Assert
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
var webApp = WebApplication.Create();
webApp.Use(async (HttpContext context, Func<Task> next) =>
{
context.SetEndpoint(new Endpoint(c => throw new Exception(), EndpointMetadataCollection.Empty, ""Test""));
await next();
});
");
}

[Fact]
public async Task AnonymousDelegate_RequestDelegate_ReturnNull_NoDiagnostics()
{
// Arrange & Act & Assert
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
var webApp = WebApplication.Create();
webApp.Use(async (HttpContext context, Func<Task> next) =>
{
context.SetEndpoint(new Endpoint(c => null, EndpointMetadataCollection.Empty, ""Test""));
await next();
});
");
}

[Fact]
public async Task AnonymousDelegate_RequestDelegate_ReturnType_EndpointCtor_ReportDiagnostics()
{
Expand Down

0 comments on commit 0cfd468

Please sign in to comment.