@@ -168,6 +168,12 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
168
168
responseType = awaitableInfo . ResultType ;
169
169
}
170
170
171
+ // Can't determine anything about IResults yet that's not from extra metadata. IResult<T> could help here.
172
+ if ( typeof ( IResult ) . IsAssignableFrom ( responseType ) )
173
+ {
174
+ responseType = typeof ( void ) ;
175
+ }
176
+
171
177
var responseMetadata = endpointMetadata . GetOrderedMetadata < IApiResponseMetadataProvider > ( ) ;
172
178
var errorMetadata = endpointMetadata . GetMetadata < ProducesErrorResponseTypeAttribute > ( ) ;
173
179
var defaultErrorType = errorMetadata ? . Type ?? typeof ( void ) ;
@@ -179,7 +185,12 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
179
185
{
180
186
foreach ( var apiResponseType in responseMetadataTypes )
181
187
{
182
- AddApiResponseFormats ( apiResponseType . ApiResponseFormats , contentTypes ) ;
188
+ if ( apiResponseType . ApiResponseFormats . Count == 0 &&
189
+ CreateDefaultApiResponseFormat ( responseType ) is { } defaultResponseFormat )
190
+ {
191
+ apiResponseType . ApiResponseFormats . Add ( defaultResponseFormat ) ;
192
+ }
193
+
183
194
supportedResponseTypes . Add ( apiResponseType ) ;
184
195
}
185
196
}
@@ -190,57 +201,47 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
190
201
191
202
if ( contentTypes . Count > 0 )
192
203
{
193
- // If metadata provided us with response formats, use that instead of the defaults .
204
+ // If metadata provided us with response formats, use that instead of the default .
194
205
defaultApiResponseType . ApiResponseFormats . Clear ( ) ;
195
- AddApiResponseFormats ( defaultApiResponseType . ApiResponseFormats , contentTypes ) ;
206
+ ApiResponseTypeProvider . AddApiResponseFormats ( defaultApiResponseType . ApiResponseFormats , contentTypes ) ;
196
207
}
197
208
198
209
supportedResponseTypes . Add ( defaultApiResponseType ) ;
199
210
}
200
211
}
201
212
202
213
private static ApiResponseType CreateDefaultApiResponseType ( Type responseType )
214
+ {
215
+ var apiResponseType = new ApiResponseType
216
+ {
217
+ ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( responseType ) ) ,
218
+ StatusCode = 200 ,
219
+ } ;
220
+
221
+ if ( CreateDefaultApiResponseFormat ( responseType ) is { } responseFormat )
222
+ {
223
+ apiResponseType . ApiResponseFormats . Add ( responseFormat ) ;
224
+ }
225
+
226
+ return apiResponseType ;
227
+ }
228
+
229
+ private static ApiResponseFormat ? CreateDefaultApiResponseFormat ( Type responseType )
203
230
{
204
231
if ( responseType == typeof ( void ) || typeof ( IResult ) . IsAssignableFrom ( responseType ) )
205
232
{
206
- // Can't determine anything about IResults yet that's not from extra metadata. IResult<T> could help here.
207
- return new ApiResponseType
208
- {
209
- ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( typeof ( void ) ) ) ,
210
- StatusCode = 200 ,
211
- } ;
233
+ return null ;
212
234
}
213
235
else if ( responseType == typeof ( string ) )
214
236
{
215
237
// This uses HttpResponse.WriteAsync(string) method which doesn't set a content type. It could be anything,
216
238
// but I think "text/plain" is a reasonable assumption if nothing else is specified with metadata.
217
- return new ApiResponseType
218
- {
219
- ApiResponseFormats = { new ApiResponseFormat { MediaType = "text/plain" } } ,
220
- ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( typeof ( string ) ) ) ,
221
- StatusCode = 200 ,
222
- } ;
239
+ return new ApiResponseFormat { MediaType = "text/plain" } ;
223
240
}
224
241
else
225
242
{
226
243
// Everything else is written using HttpResponse.WriteAsJsonAsync<TValue>(T).
227
- return new ApiResponseType
228
- {
229
- ApiResponseFormats = { new ApiResponseFormat { MediaType = "application/json" } } ,
230
- ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( responseType ) ) ,
231
- StatusCode = 200 ,
232
- } ;
233
- }
234
- }
235
-
236
- private static void AddApiResponseFormats ( IList < ApiResponseFormat > apiResponseFormats , MediaTypeCollection contentTypes )
237
- {
238
- foreach ( var contentType in contentTypes )
239
- {
240
- apiResponseFormats . Add ( new ApiResponseFormat
241
- {
242
- MediaType = contentType ,
243
- } ) ;
244
+ return new ApiResponseFormat { MediaType = "application/json" } ;
244
245
}
245
246
}
246
247
}
0 commit comments