Produces generic variants of ProducesProblem
extension methods #56178
Closed
Description
Background and Motivation
The ProducesProblem
extension method can be specified on a RouteHandlerBuilder
to specify that the route handler may return a ProblemDetails response. But unlike similar extension methods to add endpoint metadata, e.g. WithDescription
or WithTags
, the ProducesProblem
extension method is not supported on RouteGroupBuilder
. Since nearly every operation can fail in some way or other, it seems desirable to be able to easily mark all route handlers in a group as potentially returning ProblemDetails, and doing this with the ProducesProblem
method on the RouteGroupBuilder
is the most natural way to do this.
Proposed API
// Assembly: Microsoft.AspNetCore.Routing
namespace Microsoft.AspNetCore.Http;
public static class OpenApiRouteHandlerBuilderExtensions
{
+ public static TBuilder ProducesProblem<TBuilder>(this TBuilder builder, int statusCode, string? contentType = null) where TBuilder : IEndpointConventionBuilder { }
+ public static ProducesValidationProblem<TBuilder>(this TBuilder builder, int statusCode = StatusCodes.Status400BadRequest, string? contentType = null) where TBuilder : IEndpointConventionBuilder { }
}
Usage Examples
var app = WebApplication.Create();
var todos = app.MapGroup("/todos")
.ProducesProblem(statusCode: 500, "application/json+problem");