Skip to content

Commit 122f277

Browse files
committed
Set ModelMetadata when given response type for attribute
1 parent 0ff62f5 commit 122f277

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/Mvc/Mvc.ApiExplorer/src/ApiResponseTypeProvider.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ internal static List<ApiResponseType> ReadResponseMetadata(
159159

160160
if (apiResponseType.Type != null)
161161
{
162-
AddApiResponseFormats(apiResponseType.ApiResponseFormats, contentTypes);
163162
results[apiResponseType.StatusCode] = apiResponseType;
164163
}
165164
}
@@ -168,17 +167,6 @@ internal static List<ApiResponseType> ReadResponseMetadata(
168167
return results.Values.ToList();
169168
}
170169

171-
internal static void AddApiResponseFormats(IList<ApiResponseFormat> apiResponseFormats, IReadOnlyList<string> contentTypes)
172-
{
173-
foreach (var contentType in contentTypes)
174-
{
175-
apiResponseFormats.Add(new ApiResponseFormat
176-
{
177-
MediaType = contentType,
178-
});
179-
}
180-
}
181-
182170
private void CalculateResponseFormats(ICollection<ApiResponseType> responseTypes, MediaTypeCollection declaredContentTypes)
183171
{
184172
var responseTypeMetadataProviders = _mvcOptions.OutputFormatters.OfType<IApiResponseTypeMetadataProvider>();
@@ -194,9 +182,6 @@ private void CalculateResponseFormats(ICollection<ApiResponseType> responseTypes
194182

195183
foreach (var apiResponse in responseTypes)
196184
{
197-
// ApiResponseFormats has been preset for EndpointMetadataApiDescriptionProvider's use but MVC has more inputs to consider.
198-
apiResponse.ApiResponseFormats.Clear();
199-
200185
var responseType = apiResponse.Type;
201186
if (responseType == null || responseType == typeof(void))
202187
{

src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Reflection;
89
using 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
}

src/Mvc/Mvc.ApiExplorer/test/EndpointMethodInfoApiDescriptionProviderTest.cs renamed to src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Microsoft.AspNetCore.Mvc.ApiExplorer
1515
{
16-
public class EndpointMethodInfoApiDescriptionProviderTest
16+
public class EndpointMetadataApiDescriptionProviderTest
1717
{
1818
[Fact]
1919
public void ApiDescription_MultipleCreatedForMultipleHttpMethods()

0 commit comments

Comments
 (0)