Skip to content

Commit 0109916

Browse files
committed
AddSupportedRequestFormats
1 parent 122f277 commit 0109916

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/Mvc/Mvc.ApiExplorer/src/DefaultApiDescriptionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ private IReadOnlyList<ApiRequestFormat> GetSupportedFormats(MediaTypeCollection
429429
return results;
430430
}
431431

432-
private static MediaTypeCollection GetDeclaredContentTypes(IApiRequestMetadataProvider[]? requestMetadataAttributes)
432+
internal static MediaTypeCollection GetDeclaredContentTypes(IReadOnlyList<IApiRequestMetadataProvider>? requestMetadataAttributes)
433433
{
434434
// Walk through all 'filter' attributes in order, and allow each one to see or override
435435
// the results of the previous ones. This is similar to the execution path for content-negotiation.

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,7 @@ private static ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint,
8787
apiDescription.ParameterDescriptions.Add(parameterDescription);
8888
}
8989

90-
if (hasJsonBody)
91-
{
92-
apiDescription.SupportedRequestFormats.Add(new ApiRequestFormat
93-
{
94-
MediaType = "application/json",
95-
});
96-
}
97-
90+
AddSupportedRequestFormats(apiDescription.SupportedRequestFormats, hasJsonBody, routeEndpoint.Metadata);
9891
AddSupportedResponseTypes(apiDescription.SupportedResponseTypes, methodInfo.ReturnType, routeEndpoint.Metadata);
9992

10093
return apiDescription;
@@ -160,7 +153,35 @@ private static (BindingSource, string) GetBindingSourceAndName(ParameterInfo par
160153
}
161154
}
162155

163-
private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedResponseTypes, Type returnType, EndpointMetadataCollection endpointMetadata)
156+
private static void AddSupportedRequestFormats(
157+
IList<ApiRequestFormat> supportedRequestFormats,
158+
bool hasJsonBody,
159+
EndpointMetadataCollection endpointMetadata)
160+
{
161+
// If RequestDelegateFactory thinks the API supports a JSON body, it does.
162+
if (hasJsonBody)
163+
{
164+
supportedRequestFormats.Add(new ApiRequestFormat
165+
{
166+
MediaType = "application/json",
167+
});
168+
}
169+
170+
var requestMetadata = endpointMetadata.GetOrderedMetadata<IApiRequestMetadataProvider>();
171+
172+
foreach (var contentType in DefaultApiDescriptionProvider.GetDeclaredContentTypes(requestMetadata))
173+
{
174+
supportedRequestFormats.Add(new ApiRequestFormat
175+
{
176+
MediaType = contentType,
177+
});
178+
}
179+
}
180+
181+
private static void AddSupportedResponseTypes(
182+
IList<ApiResponseType> supportedResponseTypes,
183+
Type returnType,
184+
EndpointMetadataCollection endpointMetadata)
164185
{
165186
var responseType = returnType;
166187

@@ -180,7 +201,8 @@ private static void AddSupportedResponseTypes(IList<ApiResponseType> supportedRe
180201
var defaultErrorType = errorMetadata?.Type ?? typeof(void);
181202
var contentTypes = new MediaTypeCollection();
182203

183-
var responseMetadataTypes = ApiResponseTypeProvider.ReadResponseMetadata(responseMetadata, responseType, defaultErrorType, contentTypes);
204+
var responseMetadataTypes = ApiResponseTypeProvider.ReadResponseMetadata(
205+
responseMetadata, responseType, defaultErrorType, contentTypes);
184206

185207
if (responseMetadataTypes.Count > 0)
186208
{
@@ -236,7 +258,7 @@ private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
236258

237259
private static ApiResponseFormat? CreateDefaultApiResponseFormat(Type responseType)
238260
{
239-
if (responseType == typeof(void) || typeof(IResult).IsAssignableFrom(responseType))
261+
if (responseType == typeof(void))
240262
{
241263
return null;
242264
}

0 commit comments

Comments
 (0)