Skip to content

Produces generic variants of ProducesProblem extension methods #56178

Closed
@mikekistler

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");

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions