Skip to content

Commit 1eefa01

Browse files
committed
Add OpenAPI extension methods to resolve #33924
1 parent 74df1e3 commit 1eefa01

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Builder;
3+
4+
namespace Microsoft.AspNetCore.Http
5+
{
6+
/// <summary>
7+
/// Extension methods for adding response type metadata to endpoints.
8+
/// </summary>
9+
public static class OpenApiEndpointConventionBuilderExtensions
10+
{
11+
/// <summary>
12+
/// Adds metadata indicating the type of response an endpoint produces.
13+
/// </summary>
14+
/// <typeparam name="TResponse">The type of the response.</typeparam>
15+
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
16+
/// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status200OK.</param>
17+
/// <param name="contentType">The response content type. Defaults to "application/json"</param>
18+
/// <param name="additionalContentTypes">Additional response content types the endpoint produces for the supplied status code.</param>
19+
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
20+
public static IEndpointConventionBuilder Produces<TResponse>(this IEndpointConventionBuilder builder,
21+
int statusCode = StatusCodes.Status200OK,
22+
string? contentType = "application/json",
23+
params string[] additionalContentTypes)
24+
{
25+
return Produces(builder, statusCode, typeof(TResponse), contentType, additionalContentTypes);
26+
}
27+
28+
/// <summary>
29+
/// Adds metadata indicating the type of response an endpoint produces.
30+
/// </summary>
31+
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
32+
/// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status200OK.</param>
33+
/// <param name="responseType">The type of the response. Defaults to null.</param>
34+
/// <param name="contentType">The response content type. Defaults to "application/json" if responseType is not null, otherwise defaults to null.</param>
35+
/// <param name="additionalContentTypes">Additional response content types the endpoint produces for the supplied status code.</param>
36+
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
37+
public static IEndpointConventionBuilder Produces(this IEndpointConventionBuilder builder,
38+
int statusCode = StatusCodes.Status200OK,
39+
Type? responseType = null,
40+
string? contentType = null,
41+
params string[] additionalContentTypes)
42+
{
43+
if (responseType is Type && string.IsNullOrEmpty(contentType))
44+
{
45+
contentType = "application/json";
46+
}
47+
48+
builder.WithMetadata(new ProducesResponseTypeAttribute(responseType ?? typeof(void), statusCode, contentType, additionalContentTypes));
49+
50+
return builder;
51+
}
52+
53+
/// <summary>
54+
/// Adds metadata indicating that the endpoint produces a Problem Details response.
55+
/// </summary>
56+
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
57+
/// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status500InternalServerError.</param>
58+
/// <param name="contentType">The response content type. Defaults to "application/problem+json".</param>
59+
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
60+
public static IEndpointConventionBuilder ProducesProblem(this IEndpointConventionBuilder builder,
61+
int statusCode = StatusCodes.Status500InternalServerError,
62+
string contentType = "application/problem+json")
63+
{
64+
return Produces<ProblemDetails>(builder, statusCode, contentType);
65+
}
66+
67+
/// <summary>
68+
/// Adds metadata indicating that the endpoint produces a ProblemDetails response for validation errors.
69+
/// </summary>
70+
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
71+
/// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status400BadRequest.</param>
72+
/// <param name="contentType">The response content type. Defaults to "application/problem+json".</param>
73+
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
74+
public static IEndpointConventionBuilder ProducesValidationProblem(this IEndpointConventionBuilder builder,
75+
int statusCode = StatusCodes.Status400BadRequest,
76+
string contentType = "application/problem+json")
77+
{
78+
return Produces<HttpValidationProblemDetails>(builder, statusCode, contentType);
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)