dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.56k stars 10.05k forks source link

Type mismatches between parameter and route constraints not caught #36859

Open captainsafia opened 3 years ago

captainsafia commented 3 years ago

In .NET 6, we added support for an analyzer that would detect the incompat between optionality annotations on the route constraint and parameter optionality in a minimal endpoint. See https://github.com/dotnet/aspnetcore/issues/34553 for more info.

We don't currently do anything about mismatches in the type constraints between a route and parameter, so having an endpoint like:

app.MapGet("/workouts/{id:int}", async (string id, IWorkoutsService workouts) => {
    return await workouts.GetItemAsync(id);
})

Will compile and build fine but sending the following request:

$ http http://localhost:5000/workouts/thisisastring

Will result in a 404 during the route matching phase without any warning to the user.

ghost commented 2 years ago

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

captainsafia commented 2 years ago

@JamesNK Is this something the new routing tooling addresses?

JamesNK commented 2 years ago

It doesn't right now. But the route tooling infrastructure should make this analyzer quick to write:

  1. Query analyzers from route parser.
  2. Get a list of all parameters from usage context.
  3. Compare and report warnings as needed.