-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Auto revolve open-api docs from JsonHttpResult<TValue> type #47630
Comments
The One thing to note here is that the What kind of API are you using |
Hi @mehdihadeli. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Thanks for your response. That was my mistake, I mean public static RouteHandlerBuilder MapCommandEndpoint<TRequest, TResponse, TCommand, TCommandResult>(
this IEndpointRouteBuilder builder,
string pattern,
int statusCode,
Func<TRequest, TCommand>? mapRequestToCommand = null,
Func<TCommandResult, TResponse>? mapCommandResultToResponse = null
)
where TRequest : class
where TResponse : class
where TCommandResult : class
where TCommand : IRequest<TCommandResult>
{
return builder.MapPost(pattern, Handle);
async Task<JsonHttpResult<TResponse>> Handle([AsParameters] HttpCommand<TRequest> requestParameters)
{
var (request, context, mediator, mapper, cancellationToken) = requestParameters;
var command = mapRequestToCommand is not null ?
mapRequestToCommand(request) :
mapper.Map<TCommand>(request);
var res = await mediator.Send(command, cancellationToken);
var response = mapCommandResultToResponse is not null ?
mapCommandResultToResponse(res) :
mapper.Map<TResponse>(res);
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/responses
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/openapi?view=aspnetcore-7.0#multiple-response-types
return TypedResults.Json(response, statusCode: statusCode);
}
} |
@mehdihadeli what were you hoping for in the current situation. You are returning a JsonHttpResult from the method with the status code being dynamically set at runtime. The Swagger document produced includes the endpoint but can't provide more detail about the status codes because that information isn't available at that point. You might want to look at mapping the range of possible status codes to a strong type like the following so that the field of possible results can be inferred: app.MapGet("/", async Task<Results<Ok<string>, NotFound<string>>> (string response) => {
return response switch
{
"200" => TypedResults.Ok<string>("foo"),
_ => TypedResults.NotFound<string>("bar"),
};
}); |
Just to generalize my response type, because in my endpoint there are multiple possibilities for the result, and putting all them in the multiple Results<,> is one approach, but I think |
Not quite. The To conceptualize this a bit better imagine if your code instead set the value of By using Hope this makes sense. |
Hi @mehdihadeli. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Thanks for your explanation, so this is not possible because public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
{
builder.Metadata.Add(new ResponseMetadata(???, typeof(ProblemDetails)));
} |
Thats right. The purpose of |
Thanks |
Hi,
Currently .net core 7 doesn't support, auto revolve open-api docs from
JsonResult
type because it doesn't implementIEndpointMetadataProvider
. Could you consider this in the next version?The text was updated successfully, but these errors were encountered: