-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add TypedResults static factory class for creating typed IResult objects #41161
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
Conversation
1b75667
to
2c93eca
Compare
Here's a bit of a compromise on what I said about conflicts. We change all of the classes to verbs instead of nouns and keep the all without the result/httpresult. Thoughts? |
Example? Like The other thought that came up last week was keeping the using OkTodo = Microsoft.AspNetCore.Http.HttpResults.OkHttpResult<Todo>;
using NotFound = Microsoft.AspNetCore.Http.HttpResults.NotFoundHttpResult;
app.MapGet("/todo/{id}", async Task<Results<OkTodo, NotFound>> (int id, TodoDb db) =>
await db.Todos.FindAsync(id) is Todo todo
? new OkTodo(todo)
: new NotFound(); |
Seems a little bit weird have verbs in the class names and it does not sound better that *HttpResult. I know that might be less clear but what about an alternative name? Eg. Ok -> Succeeded |
Kinda hate it 😄 Sticking with well established names for HTTP statuses is the only logical thing to do I think. |
To be honest I hate it as well 😂 |
- Reinstate the `HttpResult` suffix on IResult types except for where the short name has recognized value, e.g. those types that implement `IEndpointMetadataProvider` and will be used in conjunction with `Results<T1, TN>` - Order `using` statements before `namespace` declarations - Change from `Results.Typed.xxx` to `TypedResults.xxx` - Added `HttpResults.ValidationProblem` type that represents 400 responses with validation errors (equiv to `BadRequest<HttpValidationProblemDetails>` - Changed `TypedResults.ValidationProblem` to return new `HttpResults.ValidationProblem` type - Moved `Results<TResult1, TResultN>` types into the `Microsoft.AspNetCore.Http.HttpResults` namespace to match where the other results types are
2a5c30f
to
f551d7b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't go over the new APIs with a fine-toothed comb, but I'm assuming they are what we approved.
Thanks for the new tests for the existing types :)
/// <summary> | ||
/// An <see cref="IResult"/> that on execution will write an object to the response | ||
/// with Unprocessable Entity (422) status code. | ||
/// </summary> | ||
public sealed class UnprocessableEntityObjectHttpResult : IResult | ||
public sealed class UnprocessableEntity : IResult, IEndpointMetadataProvider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DamianEdwards Is it too late to rename these to Unprocessable Content
to match the new official reason phrase from RFC9110?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proposal; main...khellang:unprocessable-content
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also filed #42302
HttpResult
suffix onIResult
types except for where the short name has recognized value, e.g. those types that implementIEndpointMetadataProvider
and will be used in conjunction withResults<T1, TN>
, e.g.Results<Ok<Todo>, NotFound>
Results.Typed.xxx
toTypedResults.xxx
HttpResults.ValidationProblem
type that represents 400 responses with validation errors (equiv toBadRequest<HttpValidationProblemDetails>
TypedResults.ValidationProblem
to return newHttpResults.ValidationProblem
typeResults<TResult1, TResultN>
types into theMicrosoft.AspNetCore.Http.HttpResults
namespace to match where the other results types areResults.xxx
methods to call through toTypedResults.xxx
methodsIEndpointMetadataProvider
on followingIResult
types:Accepted
Accepted<TValue>
AcceptedAtRoute
AcceptedAtRoute<TValue>
BadRequest
BadRequest<TValue>
Conflict
Conflict<TValue>
Created
Created<TValue>
CreatedAtRoute
CreatedAtRoute<TValue>
NoContent
NotFound
NotFound<TValue>
Ok
Ok<TValue>
UnprocessableEntity
UnprocessableEntity<TValue>
using
statements beforenamespace
declarationsMicrosoft.AspNetCore.Http.Results
andMicrosoft.AspNetCore.Http.TypedResults
Fixes #41009