3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Diagnostics ;
6
7
using System . Linq ;
7
8
using System . Reflection ;
8
9
using System . Threading ;
@@ -106,7 +107,7 @@ private static ApiParameterDescription CreateApiParameterDescription(ParameterIn
106
107
return new ApiParameterDescription
107
108
{
108
109
Name = name ,
109
- ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( parameter . ParameterType ) ) ,
110
+ ModelMetadata = CreateModelMetadata ( parameter . ParameterType ) ,
110
111
Source = source ,
111
112
DefaultValue = parameter . DefaultValue ,
112
113
} ;
@@ -185,8 +186,15 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
185
186
{
186
187
foreach ( var apiResponseType in responseMetadataTypes )
187
188
{
188
- if ( apiResponseType . ApiResponseFormats . Count == 0 &&
189
- CreateDefaultApiResponseFormat ( responseType ) is { } defaultResponseFormat )
189
+ Debug . Assert ( apiResponseType . Type is not null , "ApiResponseTypeProvider gave us a null Type!?" ) ;
190
+
191
+ apiResponseType . ModelMetadata = CreateModelMetadata ( apiResponseType . Type ) ;
192
+
193
+ if ( contentTypes . Count > 0 )
194
+ {
195
+ AddResponseContentTypes ( apiResponseType . ApiResponseFormats , contentTypes ) ;
196
+ }
197
+ else if ( CreateDefaultApiResponseFormat ( responseType ) is { } defaultResponseFormat )
190
198
{
191
199
apiResponseType . ApiResponseFormats . Add ( defaultResponseFormat ) ;
192
200
}
@@ -203,7 +211,7 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
203
211
{
204
212
// If metadata provided us with response formats, use that instead of the default.
205
213
defaultApiResponseType . ApiResponseFormats . Clear ( ) ;
206
- ApiResponseTypeProvider . AddApiResponseFormats ( defaultApiResponseType . ApiResponseFormats , contentTypes ) ;
214
+ AddResponseContentTypes ( defaultApiResponseType . ApiResponseFormats , contentTypes ) ;
207
215
}
208
216
209
217
supportedResponseTypes . Add ( defaultApiResponseType ) ;
@@ -214,7 +222,7 @@ private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
214
222
{
215
223
var apiResponseType = new ApiResponseType
216
224
{
217
- ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( responseType ) ) ,
225
+ ModelMetadata = CreateModelMetadata ( responseType ) ,
218
226
StatusCode = 200 ,
219
227
} ;
220
228
@@ -244,5 +252,19 @@ private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
244
252
return new ApiResponseFormat { MediaType = "application/json" } ;
245
253
}
246
254
}
255
+
256
+ private static EndpointModelMetadata CreateModelMetadata ( Type type ) =>
257
+ new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( type ) ) ;
258
+
259
+ private static void AddResponseContentTypes ( IList < ApiResponseFormat > apiResponseFormats , IReadOnlyList < string > contentTypes )
260
+ {
261
+ foreach ( var contentType in contentTypes )
262
+ {
263
+ apiResponseFormats . Add ( new ApiResponseFormat
264
+ {
265
+ MediaType = contentType ,
266
+ } ) ;
267
+ }
268
+ }
247
269
}
248
270
}
0 commit comments