Skip to content

[Analyzer] RequestDelegate return value detection #44316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JamesNK opened this issue Oct 3, 2022 · 7 comments · Fixed by #44393
Closed

[Analyzer] RequestDelegate return value detection #44316

JamesNK opened this issue Oct 3, 2022 · 7 comments · Fixed by #44393
Labels
api-approved API was approved in API review, it can be implemented

Comments

@JamesNK
Copy link
Member

JamesNK commented Oct 3, 2022

Background and Motivation

RequestDelegate can be used with methods/lambdas that return a value that is then ignored.

Delegate signature:

/// <summary>
/// A function that can process an HTTP request.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the request.</param>
/// <returns>A task that represents the completion of request processing.</returns>
public delegate Task RequestDelegate(HttpContext context);

Because Task is the base type of Task<T>, generic variance means it's possible to return Task<T> from a method that's used with RequestDelegate. The caller of the delegate never sees the value and it is ignored.

Example: #39956

Proposed API

An analyzer that detects using a method or lambda that returns Task<T> with RequestDelegate and warns the user.

Usage Examples

Task<string> HelloWorld(HttpContext c) => Task.FromResult("Hello " + c.Request.RouteValues["name"]);

endpoints.MapGet("/", HelloWorld); // <- detect bad method

endpoints.MapGet("/", (HttpContext c) => Task.FromResult("Hello " + c.Request.RouteValues["name"])); // <- detect bad lambda

Note that an async lambda can't get in this situation and doesn't need to be checked. The compiler detects a return value and prevents the lambda being converted to RequestDelegate:

RequestDelegate d = new RequestDelegate(async Task<string> (HttpContext context) =>
{
    await Task.Yield();
    return "hello world"; // <- compiler error
});

Alternative Designs

Risks

@JamesNK JamesNK added api-suggestion Early API idea and discussion, it is NOT ready for implementation api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Oct 3, 2022
@ghost
Copy link

ghost commented Oct 3, 2022

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@davidfowl
Copy link
Member

What does a "bad method" mean? We fixed this in .NET 7.

@JamesNK
Copy link
Member Author

JamesNK commented Oct 3, 2022

A RequestDelegate method that returns Task<T>.

@davidfowl
Copy link
Member

That works now. This is why the issue is closed.

@JamesNK
Copy link
Member Author

JamesNK commented Oct 3, 2022

The change was reverted later - #42519

@davidfowl
Copy link
Member

davidfowl commented Oct 3, 2022

So is this about trimming or something else? The change wasn't reverted, just the trimming attributes.

@halter73 halter73 added api-approved API was approved in API review, it can be implemented and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Oct 5, 2022
@halter73
Copy link
Member

halter73 commented Oct 5, 2022

API Review Notes:

  • This especially important since this isn't caught at runtime and is likely misusage.

Category: Usage
Severity: Warning

@ghost ghost locked as resolved and limited conversation to collaborators Nov 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants