dotnet / dotnet/aspnetcore

Minimal APIs result processing does not support IResult via polymorphism unless an endpoint filter is added

Open
#46,599 1 comment 0 reactions 0 assignees View on GitHub
area-minimal feature-minimal-actions
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

Processing of `IResult` instances returned from route handler delegates in minimal APIs only takes into the consideration the declared return type of the delegate. The example below will result in the returned object being JSON serialized, rather than the `DerivedType.ExecuteAsync` method being called.

However, adding a no-op passthrough filter to this endpoint will change the return object processing behavior to use the runtime type instead.

This different behavior is hard to reason about. Consider a scenario where an endpoint is added with a filter initially, and as a result will have its results processed as `IResult`, and then if the filter is later removed the results will now be JSON serialized.

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

app.MapGet("/", BaseType () => new DerivedType());
app.Run();

class BaseType { }
class DerivedType : BaseType, IResult
{
public Task ExecuteAsync(HttpContext httpContext)
{
throw new NotImplementedException();
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.