dotnet / dotnet/aspnetcore

Analyzer: Warn when passing a Task<T> or ValueTask<T> to Minimal API Results.* methods

Open
#36,408 2 comments 4 reactions 0 assignees View on GitHub
analyzer area-minimal feature-minimal-actions
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is your feature request related to a problem? Please describe.

Playing around with Minimal APIs, I wrote some code similar to the below without noticing I'd forgotten to await the method:

```csharp
app.MapGet("/do-thing", (IThingDoer thing) => Results.Json(thing.DoThingAsync()));
```

Calling `/do-thing` then returned some JSON similar to the below:

```json
{"result":{},"id":1,"exception":null,"status":5,"isCanceled":false,"isCompleted":true,"isCompletedSuccessfully":true,"creationOptions":0,"asyncState":null,"isFaulted":false}
```

Easily fixed by updating the method signature appropriately, but an easy enough mistake to make.

```csharp
app.MapGet("/do-thing", async (IThingDoer thing) => Results.Json(await thing.DoThingAsync()));
```

### Describe the solution you'd like

An analyzer that warns if a "task-like" type (`Task`, `Task`, `ValueTask`, `ValueTask`) is passed to one of the static `Results` methods (like `Results.Json(object? data, ...)`) for a parameter of type `object?`.

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.