@@ -16,18 +16,23 @@ public static class OpenApiRouteHandlerBuilderExtensions
16
16
{
17
17
private static readonly ExcludeFromDescriptionAttribute _excludeFromDescriptionMetadataAttribute = new ( ) ;
18
18
19
+ /// <summary>
20
+ /// Adds the <see cref="IExcludeFromDescriptionMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
21
+ /// produced by <paramref name="builder"/>.
22
+ /// </summary>
23
+ /// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
24
+ /// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
25
+ public static TBuilder ExcludeFromDescription < TBuilder > ( this TBuilder builder ) where TBuilder : IEndpointConventionBuilder
26
+ => builder . WithMetadata ( _excludeFromDescriptionMetadataAttribute ) ;
27
+
19
28
/// <summary>
20
29
/// Adds the <see cref="IExcludeFromDescriptionMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
21
30
/// produced by <paramref name="builder"/>.
22
31
/// </summary>
23
32
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
24
33
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
25
34
public static RouteHandlerBuilder ExcludeFromDescription ( this RouteHandlerBuilder builder )
26
- {
27
- builder . WithMetadata ( _excludeFromDescriptionMetadataAttribute ) ;
28
-
29
- return builder ;
30
- }
35
+ => ExcludeFromDescription < RouteHandlerBuilder > ( builder ) ;
31
36
32
37
/// <summary>
33
38
/// Adds an <see cref="IProducesResponseTypeMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
@@ -40,9 +45,10 @@ public static RouteHandlerBuilder ExcludeFromDescription(this RouteHandlerBuilde
40
45
/// <param name="additionalContentTypes">Additional response content types the endpoint produces for the supplied status code.</param>
41
46
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
42
47
#pragma warning disable RS0026
43
- public static RouteHandlerBuilder Produces < TResponse > ( this RouteHandlerBuilder builder ,
48
+ public static RouteHandlerBuilder Produces < TResponse > (
44
49
#pragma warning restore RS0026
45
- int statusCode = StatusCodes . Status200OK ,
50
+ this RouteHandlerBuilder builder ,
51
+ int statusCode = StatusCodes . Status200OK ,
46
52
string ? contentType = null ,
47
53
params string [ ] additionalContentTypes )
48
54
{
@@ -60,9 +66,10 @@ public static RouteHandlerBuilder Produces<TResponse>(this RouteHandlerBuilder b
60
66
/// <param name="additionalContentTypes">Additional response content types the endpoint produces for the supplied status code.</param>
61
67
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
62
68
#pragma warning disable RS0026
63
- public static RouteHandlerBuilder Produces ( this RouteHandlerBuilder builder ,
69
+ public static RouteHandlerBuilder Produces (
64
70
#pragma warning restore RS0026
65
- int statusCode ,
71
+ this RouteHandlerBuilder builder ,
72
+ int statusCode ,
66
73
Type ? responseType = null ,
67
74
string ? contentType = null ,
68
75
params string [ ] additionalContentTypes )
@@ -74,13 +81,10 @@ public static RouteHandlerBuilder Produces(this RouteHandlerBuilder builder,
74
81
75
82
if ( contentType is null )
76
83
{
77
- builder . WithMetadata ( new ProducesResponseTypeMetadata ( responseType ?? typeof ( void ) , statusCode ) ) ;
78
- return builder ;
84
+ return builder . WithMetadata ( new ProducesResponseTypeMetadata ( responseType ?? typeof ( void ) , statusCode ) ) ;
79
85
}
80
86
81
- builder . WithMetadata ( new ProducesResponseTypeMetadata ( responseType ?? typeof ( void ) , statusCode , contentType , additionalContentTypes ) ) ;
82
-
83
- return builder ;
87
+ return builder . WithMetadata ( new ProducesResponseTypeMetadata ( responseType ?? typeof ( void ) , statusCode , contentType , additionalContentTypes ) ) ;
84
88
}
85
89
86
90
/// <summary>
@@ -91,16 +95,14 @@ public static RouteHandlerBuilder Produces(this RouteHandlerBuilder builder,
91
95
/// <param name="statusCode">The response status code.</param>
92
96
/// <param name="contentType">The response content type. Defaults to "application/problem+json".</param>
93
97
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
94
- public static RouteHandlerBuilder ProducesProblem ( this RouteHandlerBuilder builder ,
95
- int statusCode ,
96
- string ? contentType = null )
98
+ public static RouteHandlerBuilder ProducesProblem ( this RouteHandlerBuilder builder , int statusCode , string ? contentType = null )
97
99
{
98
100
if ( string . IsNullOrEmpty ( contentType ) )
99
101
{
100
102
contentType = "application/problem+json" ;
101
103
}
102
104
103
- return Produces < ProblemDetails > ( builder , statusCode , contentType ) ;
105
+ return Produces ( builder , statusCode , typeof ( ProblemDetails ) , contentType ) ;
104
106
}
105
107
106
108
/// <summary>
@@ -111,7 +113,8 @@ public static RouteHandlerBuilder ProducesProblem(this RouteHandlerBuilder build
111
113
/// <param name="statusCode">The response status code. Defaults to <see cref="StatusCodes.Status400BadRequest"/>.</param>
112
114
/// <param name="contentType">The response content type. Defaults to "application/problem+json".</param>
113
115
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
114
- public static RouteHandlerBuilder ProducesValidationProblem ( this RouteHandlerBuilder builder ,
116
+ public static RouteHandlerBuilder ProducesValidationProblem (
117
+ this RouteHandlerBuilder builder ,
115
118
int statusCode = StatusCodes . Status400BadRequest ,
116
119
string ? contentType = null )
117
120
{
@@ -120,9 +123,24 @@ public static RouteHandlerBuilder ProducesValidationProblem(this RouteHandlerBui
120
123
contentType = "application/problem+json" ;
121
124
}
122
125
123
- return Produces < HttpValidationProblemDetails > ( builder , statusCode , contentType ) ;
126
+ return Produces ( builder , statusCode , typeof ( HttpValidationProblemDetails ) , contentType ) ;
124
127
}
125
128
129
+ /// <summary>
130
+ /// Adds the <see cref="ITagsMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
131
+ /// produced by <paramref name="builder"/>.
132
+ /// </summary>
133
+ /// <remarks>
134
+ /// The OpenAPI specification supports a tags classification to categorize operations
135
+ /// into related groups. These tags are typically included in the generated specification
136
+ /// and are typically used to group operations by tags in the UI.
137
+ /// </remarks>
138
+ /// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
139
+ /// <param name="tags">A collection of tags to be associated with the endpoint.</param>
140
+ /// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
141
+ public static TBuilder WithTags < TBuilder > ( this TBuilder builder , params string [ ] tags ) where TBuilder : IEndpointConventionBuilder
142
+ => builder . WithMetadata ( new TagsAttribute ( tags ) ) ;
143
+
126
144
/// <summary>
127
145
/// Adds the <see cref="ITagsMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
128
146
/// produced by <paramref name="builder"/>.
@@ -136,10 +154,7 @@ public static RouteHandlerBuilder ProducesValidationProblem(this RouteHandlerBui
136
154
/// <param name="tags">A collection of tags to be associated with the endpoint.</param>
137
155
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
138
156
public static RouteHandlerBuilder WithTags ( this RouteHandlerBuilder builder , params string [ ] tags )
139
- {
140
- builder . WithMetadata ( new TagsAttribute ( tags ) ) ;
141
- return builder ;
142
- }
157
+ => WithTags < RouteHandlerBuilder > ( builder , tags ) ;
143
158
144
159
/// <summary>
145
160
/// Adds <see cref="IAcceptsMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
@@ -150,12 +165,12 @@ public static RouteHandlerBuilder WithTags(this RouteHandlerBuilder builder, par
150
165
/// <param name="contentType">The request content type that the endpoint accepts.</param>
151
166
/// <param name="additionalContentTypes">The list of additional request content types that the endpoint accepts.</param>
152
167
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
153
- public static RouteHandlerBuilder Accepts < TRequest > ( this RouteHandlerBuilder builder ,
154
- string contentType , params string [ ] additionalContentTypes ) where TRequest : notnull
168
+ public static RouteHandlerBuilder Accepts < TRequest > (
169
+ this RouteHandlerBuilder builder ,
170
+ string contentType ,
171
+ params string [ ] additionalContentTypes ) where TRequest : notnull
155
172
{
156
- Accepts ( builder , typeof ( TRequest ) , contentType , additionalContentTypes ) ;
157
-
158
- return builder ;
173
+ return Accepts ( builder , typeof ( TRequest ) , contentType , additionalContentTypes ) ;
159
174
}
160
175
161
176
/// <summary>
@@ -168,12 +183,13 @@ public static RouteHandlerBuilder Accepts<TRequest>(this RouteHandlerBuilder bui
168
183
/// <param name="contentType">The request content type that the endpoint accepts.</param>
169
184
/// <param name="additionalContentTypes">The list of additional request content types that the endpoint accepts.</param>
170
185
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
171
- public static RouteHandlerBuilder Accepts < TRequest > ( this RouteHandlerBuilder builder ,
172
- bool isOptional , string contentType , params string [ ] additionalContentTypes ) where TRequest : notnull
186
+ public static RouteHandlerBuilder Accepts < TRequest > (
187
+ this RouteHandlerBuilder builder ,
188
+ bool isOptional ,
189
+ string contentType ,
190
+ params string [ ] additionalContentTypes ) where TRequest : notnull
173
191
{
174
- Accepts ( builder , typeof ( TRequest ) , isOptional , contentType , additionalContentTypes ) ;
175
-
176
- return builder ;
192
+ return Accepts ( builder , typeof ( TRequest ) , isOptional , contentType , additionalContentTypes ) ;
177
193
}
178
194
179
195
/// <summary>
@@ -185,11 +201,13 @@ public static RouteHandlerBuilder Accepts<TRequest>(this RouteHandlerBuilder bui
185
201
/// <param name="contentType">The request content type that the endpoint accepts.</param>
186
202
/// <param name="additionalContentTypes">The list of additional request content types that the endpoint accepts.</param>
187
203
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
188
- public static RouteHandlerBuilder Accepts ( this RouteHandlerBuilder builder ,
189
- Type requestType , string contentType , params string [ ] additionalContentTypes )
204
+ public static RouteHandlerBuilder Accepts (
205
+ this RouteHandlerBuilder builder ,
206
+ Type requestType ,
207
+ string contentType ,
208
+ params string [ ] additionalContentTypes )
190
209
{
191
- builder . WithMetadata ( new AcceptsMetadata ( requestType , false , GetAllContentTypes ( contentType , additionalContentTypes ) ) ) ;
192
- return builder ;
210
+ return Accepts ( builder , requestType , isOptional : false , contentType , additionalContentTypes ) ;
193
211
}
194
212
195
213
/// <summary>
@@ -202,38 +220,36 @@ public static RouteHandlerBuilder Accepts(this RouteHandlerBuilder builder,
202
220
/// <param name="contentType">The request content type that the endpoint accepts.</param>
203
221
/// <param name="additionalContentTypes">The list of additional request content types that the endpoint accepts.</param>
204
222
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
205
- public static RouteHandlerBuilder Accepts ( this RouteHandlerBuilder builder ,
206
- Type requestType , bool isOptional , string contentType , params string [ ] additionalContentTypes )
223
+ public static RouteHandlerBuilder Accepts (
224
+ this RouteHandlerBuilder builder ,
225
+ Type requestType ,
226
+ bool isOptional ,
227
+ string contentType ,
228
+ params string [ ] additionalContentTypes )
207
229
{
208
- builder . WithMetadata ( new AcceptsMetadata ( requestType , isOptional , GetAllContentTypes ( contentType , additionalContentTypes ) ) ) ;
209
- return builder ;
230
+ var contentTypes = GetAllContentTypes ( contentType , additionalContentTypes ) ;
231
+ return builder . WithMetadata ( new AcceptsMetadata ( requestType , isOptional , contentTypes ) ) ;
210
232
}
211
233
212
234
/// <summary>
213
235
/// Adds <see cref="IEndpointDescriptionMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
214
236
/// produced by <paramref name="builder"/>.
215
237
/// </summary>
216
- /// <param name="builder">The <see cref="RouteHandlerBuilder "/>.</param>
238
+ /// <param name="builder">The <see cref="IEndpointConventionBuilder "/>.</param>
217
239
/// <param name="description">A string representing a detailed description of the endpoint.</param>
218
240
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
219
- public static RouteHandlerBuilder WithDescription ( this RouteHandlerBuilder builder , string description )
220
- {
221
- builder . WithMetadata ( new EndpointDescriptionAttribute ( description ) ) ;
222
- return builder ;
223
- }
241
+ public static TBuilder WithDescription < TBuilder > ( this TBuilder builder , string description ) where TBuilder : IEndpointConventionBuilder
242
+ => builder . WithMetadata ( new EndpointDescriptionAttribute ( description ) ) ;
224
243
225
244
/// <summary>
226
245
/// Adds <see cref="IEndpointSummaryMetadata"/> to <see cref="EndpointBuilder.Metadata"/> for all endpoints
227
246
/// produced by <paramref name="builder"/>.
228
247
/// </summary>
229
- /// <param name="builder">The <see cref="RouteHandlerBuilder "/>.</param>
248
+ /// <param name="builder">The <see cref="IEndpointConventionBuilder "/>.</param>
230
249
/// <param name="summary">A string representing a brief description of the endpoint.</param>
231
250
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
232
- public static RouteHandlerBuilder WithSummary ( this RouteHandlerBuilder builder , string summary )
233
- {
234
- builder . WithMetadata ( new EndpointSummaryAttribute ( summary ) ) ;
235
- return builder ;
236
- }
251
+ public static TBuilder WithSummary < TBuilder > ( this TBuilder builder , string summary ) where TBuilder : IEndpointConventionBuilder
252
+ => builder . WithMetadata ( new EndpointSummaryAttribute ( summary ) ) ;
237
253
238
254
private static string [ ] GetAllContentTypes ( string contentType , string [ ] additionalContentTypes )
239
255
{
0 commit comments