1
- using Microsoft . AspNetCore . Http . Extensions ;
2
- using Microsoft . AspNetCore . Mvc ;
1
+ using Microsoft . AspNetCore . Mvc ;
2
+ using Microsoft . AspNetCore . Mvc . ApiExplorer ;
3
3
4
4
namespace Microsoft . AspNetCore . Http ;
5
5
6
- public static class OpenApiEndpointConventionBuilderExtensions
6
+ public static class EndpointConventionBuilderExtensions
7
7
{
8
- private static readonly EndpointIgnoreMetadata IgnoreMetadata = new ( ) ;
9
-
10
- /// <summary>
11
- /// Adds an EndpointNameMetadata item to the Metadata for all endpoints produced by the builder.<br />
12
- /// The name is used to lookup the endpoint during link generation and as an operationId when generating OpenAPI documentation.<br />
13
- /// The name must be unique per endpoint.
14
- /// </summary>
15
- /// <param name="builder">The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</param>
16
- /// <param name="name">The endpoint name.</param>
17
- /// <returns>The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</returns>
18
- public static IEndpointConventionBuilder WithName ( this IEndpointConventionBuilder builder , string name )
19
- {
20
- // Once Swashbuckle issue is fixed this will set operationId in the swagger doc
21
- // https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2165
22
- builder . WithMetadata ( new EndpointNameMetadata ( name ) ) ;
23
-
24
- return builder ;
25
- }
26
-
27
- /// <summary>
28
- /// Adds an EndpointGroupNameMetadata item to the Metadata for all endpoints produced by the builder.
29
- /// </summary>
30
- /// <param name="builder"></param>
31
- /// <param name="groupName"></param>
32
- /// <returns>The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</returns>
33
- public static IEndpointConventionBuilder WithGroupName ( this IEndpointConventionBuilder builder , string groupName )
8
+ public static MinimalActionEndpointConventionBuilder Accepts ( this MinimalActionEndpointConventionBuilder builder , string contentType , params string [ ] otherContentTypes )
34
9
{
35
- // Swashbuckle uses group name to match APIs with OpenApi documents by default
36
- // See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGeneratorOptions.cs#L59
37
- // Minimal APIs currently doesn't populate the ApiDescription with a group name but we will change that so this can work as intended.
38
- // Note that EndpointGroupNameMetadata doesn't exist in ASP.NET Core today so we'll have to add that too.
39
- builder . WithMetadata ( new EndpointGroupNameMetadata ( groupName ) ) ;
10
+ builder . WithMetadata ( new ConsumesAttribute ( contentType , otherContentTypes ) ) ;
40
11
41
12
return builder ;
42
13
}
43
14
44
- /// <summary>
45
- /// Adds metadata indicating an endpoint should be ignored by consumers of endpoint metadata, e.g. when generating OpenAPI documentation.
46
- /// </summary>
47
- /// <param name="builder">The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</param>
48
- /// <returns>The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</returns>
49
- public static IEndpointConventionBuilder Ignore ( this IEndpointConventionBuilder builder )
15
+ public static MinimalActionEndpointConventionBuilder Accepts < T > ( this MinimalActionEndpointConventionBuilder builder , string ? contentType = null , params string [ ] otherContentTypes )
50
16
{
51
- // Swashbuckle won't include an API in a given document if it has a group name set and it doesn't match the document name,
52
- // so setting the group name to a random value effectively hides the API from all OpenAPI documents by default
53
- // See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGeneratorOptions.cs#L59
54
- // We may instead want to add a more first-class piece of metadata to indicate the endpoint should be ignored from metadata readers,
55
- // e.g. https://github.com/dotnet/aspnetcore/issues/34068, which of course will require updating Swashbuckle to honor this too.
56
- builder . WithMetadata ( IgnoreMetadata ) ;
17
+ Accepts ( builder , typeof ( T ) , contentType , otherContentTypes ) ;
57
18
58
19
return builder ;
59
20
}
60
21
61
- /// <summary>
62
- /// Adds metadata indicating the type of response an endpoint produces.
63
- /// </summary>
64
- /// <typeparam name="TResponse">The type of the response.</typeparam>
65
- /// <param name="builder">The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</param>
66
- /// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status200OK.</param>
67
- /// <param name="contentType">The response content type. Defaults to "application/json"</param>
68
- /// <param name="additionalContentTypes">Additional response content types the endpoint produces for the supplied status code.</param>
69
- /// <returns>The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</returns>
70
- public static IEndpointConventionBuilder Produces < TResponse > ( this IEndpointConventionBuilder builder ,
71
- int statusCode = StatusCodes . Status200OK ,
72
- string ? contentType = "application/json" ,
73
- params string [ ] additionalContentTypes )
74
- {
75
- return Produces ( builder , statusCode , typeof ( TResponse ) , contentType , additionalContentTypes ) ;
76
- }
77
-
78
- /// <summary>
79
- /// Adds metadata indicating the type of response an endpoint produces.
80
- /// </summary>
81
- /// <param name="builder">The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</param>
82
- /// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status200OK.</param>
83
- /// <param name="responseType">The type of the response. Defaults to null.</param>
84
- /// <param name="contentType">The response content type. Defaults to "application/json" if responseType is not null, otherwise defaults to null.</param>
85
- /// <param name="additionalContentTypes">Additional response content types the endpoint produces for the supplied status code.</param>
86
- /// <returns>The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</returns>
87
- public static IEndpointConventionBuilder Produces ( this IEndpointConventionBuilder builder ,
88
- int statusCode = StatusCodes . Status200OK ,
89
- Type ? responseType = null ,
90
- string ? contentType = null ,
91
- params string [ ] additionalContentTypes )
22
+ public static MinimalActionEndpointConventionBuilder Accepts ( this MinimalActionEndpointConventionBuilder builder , Type requestType , string ? contentType = null , params string [ ] otherContentTypes )
92
23
{
93
- if ( responseType is Type && string . IsNullOrEmpty ( contentType ) )
94
- {
95
- contentType = "application/json" ;
96
- }
97
-
98
- builder . WithMetadata ( new ProducesMetadataAttribute ( responseType , statusCode , contentType , additionalContentTypes ) ) ;
24
+ builder . WithMetadata (
25
+ contentType is object
26
+ ? new ConsumesRequestTypeAttribute ( contentType , otherContentTypes )
27
+ {
28
+ Type = requestType
29
+ }
30
+ : new ConsumesRequestTypeAttribute ( requestType )
31
+ ) ;
99
32
100
33
return builder ;
101
34
}
102
-
103
- /// <summary>
104
- /// Adds metadata indicating that the endpoint produces a Problem Details response.
105
- /// </summary>
106
- /// <param name="builder">The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</param>
107
- /// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status500InternalServerError.</param>
108
- /// <param name="contentType">The response content type. Defaults to "application/problem+json".</param>
109
- /// <returns>The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</returns>
110
- public static IEndpointConventionBuilder ProducesProblem ( this IEndpointConventionBuilder builder ,
111
- int statusCode = StatusCodes . Status500InternalServerError ,
112
- string contentType = "application/problem+json" )
113
- {
114
- return Produces < ProblemDetails > ( builder , statusCode , contentType ) ;
115
- }
116
-
117
- /// <summary>
118
- /// Adds metadata indicating that the endpoint produces a Problem Details response including validation errors.
119
- /// </summary>
120
- /// <param name="builder">The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</param>
121
- /// <param name="statusCode">The response status code. Defatuls to StatusCodes.Status400BadRequest.</param>
122
- /// <param name="contentType">The response content type. Defaults to "application/problem+json".</param>
123
- /// <returns>The Microsoft.AspNetCore.Builder.IEndpointConventionBuilder.</returns>
124
- public static IEndpointConventionBuilder ProducesValidationProblem ( this IEndpointConventionBuilder builder ,
125
- int statusCode = StatusCodes . Status400BadRequest ,
126
- string contentType = "application/problem+json" )
127
- {
128
- return Produces < HttpValidationProblemDetails > ( builder , statusCode , contentType ) ;
129
- }
130
- }
35
+ }
0 commit comments