aspnetcore: Warning AD0001 : Analyzer 'Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer' NullReferenceException due to generics in minimal API

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

AspNetCore Analyzer throws warning due to usage of generics in minimal API:

1>CSC: Warning AD0001 : Analyzer 'Microsoft.AspNetCore.Analyzers.RouteHandlers.Route HandlerAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.

Expected Behavior

This all works fine when running it, the analyzer is just not pleased with the usage of generics. Switching TEndpointInput to a concrete type such as DummyEndpointInput of course also make the warning disappear.

Steps To Reproduce

https://github.com/madskonradsen/minimal-reproducibles/tree/main/dotnet-analyzer-warning-minimalapi-generics/MinimalAPIGenericsWarning

Or simply:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

static void UseEndpoint<TEndpointInput>(WebApplication app) where TEndpointInput : class
{
    app.MapPost("/test", (TEndpointInput data) => Results.Ok(data));
}

UseEndpoint<DummyEndpointInput>(app);

app.Run();

public class DummyEndpointInput
{
}

Exceptions (if any)

No response

.NET Version

8.0.303

Anything else?

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 7
  • Comments: 15 (6 by maintainers)

Most upvoted comments

To close the loop on this, @benhopkinstech recently investigated the root cause for this and contributed a fix that should be released in .NET 10 Preview 3.

The PR with the fix is included in https://github.com/dotnet/aspnetcore/pull/60742.

@benhopkinstech Given the number of upvotes on this issue and the relative simplicity of the fix, we should consider back porting it to .NET 8 and 9 once we’ve verified that everything works when .NET 10 Preview 3 goes out.

I found a workaround by creating a wrapper and using AsParameters attribute. Here is an example with both route parameter and body.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

static void UseEndpoint<TContent, TRouteParameter>(WebApplication app) where TContent : class
{
    app.MapPost("/test/{id}", ([AsParameters] EndpointParameters<TContent, TRouteParameter> parameters) => Results.Ok(parameters.Data));
}

UseEndpoint<DummyEndpointInput, int>(app);

app.Run();

public class DummyEndpointInput;

public struct EndpointParameters<TEndpointInput, TRouteParameter>
{
    public TEndpointInput Data { get; init; }
    public TRouteParameter Id { get; init; }
}

For route parameters, you may also want to add where TRouteParameter : IParsable<TRouteParameter> constraint.

I also think it’s worth mentioning in this thread that there is a feature flag called debug-analyzers that can be used like so: dotnet build -c Release -p:Features="debug-analyzers" (The flag somehow only consistently work when using Release configuration.)

I haven’t had much luck with the patch apart from the code above, and haven’t heard any feedback from the dotnet team.