-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Update ProducesResponseTypeAttribute to support setting content types for the defined response #34542
Copy link
Copy link
Closed
Labels
Priority:1Work that is critical for the release, but we could probably ship withoutWork that is critical for the release, but we could probably ship withoutarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-minimal-actionsController-like actions for endpoint routingController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone
Metadata
Metadata
Assignees
Labels
Priority:1Work that is critical for the release, but we could probably ship withoutWork that is critical for the release, but we could probably ship withoutarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-minimal-actionsController-like actions for endpoint routingController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Type
Fields
Give feedbackNo fields configured for issues without a type.
The
Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttributeimplementsIApiResponseMetadataProvidertoday but does not support setting the content types associated with the response type defined by the attribute instance. This limitation means there is no way to specify the content type produced for specific status codes for a given endpoint, despite the OpenAPI specification supporting this.Customers have indicated they have a need for this support in the OpenAPI library for ASP.NET Core, Swashbuckle too, see domaindrivendev/Swashbuckle.AspNetCore#1691
Take the following common example. A single endpoint can return either
400 Bad Requestwith a validation problem formatted asapplication/problem+json, or a201 Createdwith the created resource formatted asapplication/json:This would be represented in an OpenAPI document like this:
We should update
Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttributeto support defining the content types associated with the declared response type, e.g.:public class ProducesMetadataAttribute : ProducesResponseTypeAttribute, IApiResponseMetadataProvider { public ProducesMetadataAttribute(int statusCode) : base(statusCode) { } public ProducesMetadataAttribute(Type type, int statusCode) : base(type, statusCode) { } + public ProducesMetadataAttribute(Type? type, int statusCode, string? contentType, params string[] additionalContentTypes) : base(statusCode) { } + public MediaTypeCollection ContentTypes { get; set; } = new(); - void IApiResponseMetadataProvider.SetContentTypes(MediaTypeCollection contentTypes) { } + public void SetContentTypes(MediaTypeCollection contentTypes) { } }This change would work in conjunction with #33924 to enable endpoints to describe the content types they return for each status code and response type. Endpoints that don't specify a content type should default to being described as returning
application/jsoninline with the default minimal APIs behavior.