33
44using System ;
55using System . Collections . Generic ;
6+ using System . Diagnostics ;
67using System . Linq ;
78using System . Reflection ;
89using System . Threading ;
@@ -106,7 +107,7 @@ private static ApiParameterDescription CreateApiParameterDescription(ParameterIn
106107 return new ApiParameterDescription
107108 {
108109 Name = name ,
109- ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( parameter . ParameterType ) ) ,
110+ ModelMetadata = CreateModelMetadata ( parameter . ParameterType ) ,
110111 Source = source ,
111112 DefaultValue = parameter . DefaultValue ,
112113 } ;
@@ -185,8 +186,15 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
185186 {
186187 foreach ( var apiResponseType in responseMetadataTypes )
187188 {
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 )
190198 {
191199 apiResponseType . ApiResponseFormats . Add ( defaultResponseFormat ) ;
192200 }
@@ -203,7 +211,7 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
203211 {
204212 // If metadata provided us with response formats, use that instead of the default.
205213 defaultApiResponseType . ApiResponseFormats . Clear ( ) ;
206- ApiResponseTypeProvider . AddApiResponseFormats ( defaultApiResponseType . ApiResponseFormats , contentTypes ) ;
214+ AddResponseContentTypes ( defaultApiResponseType . ApiResponseFormats , contentTypes ) ;
207215 }
208216
209217 supportedResponseTypes . Add ( defaultApiResponseType ) ;
@@ -214,7 +222,7 @@ private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
214222 {
215223 var apiResponseType = new ApiResponseType
216224 {
217- ModelMetadata = new EndpointModelMetadata ( ModelMetadataIdentity . ForType ( responseType ) ) ,
225+ ModelMetadata = CreateModelMetadata ( responseType ) ,
218226 StatusCode = 200 ,
219227 } ;
220228
@@ -244,5 +252,19 @@ private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
244252 return new ApiResponseFormat { MediaType = "application/json" } ;
245253 }
246254 }
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+ }
247269 }
248270}
0 commit comments