Skip to content

Commit bc997eb

Browse files
committed
Add better type check to fix issue with description not being set
1 parent 8ba3a6e commit bc997eb

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ private static void AddSupportedResponseTypes(
322322
Type returnType,
323323
EndpointMetadataCollection endpointMetadata)
324324
{
325+
System.Diagnostics.Debugger.Break();
325326
var responseType = returnType;
326327

327328
// We support attributes (which implement the IApiResponseMetadataProvider) interface
@@ -405,14 +406,27 @@ private static void AddSupportedResponseTypes(
405406
foreach (var metadata in responseMetadataTypes)
406407
{
407408
if (metadata.StatusCode == apiResponseType.StatusCode &&
408-
metadata.Type == apiResponseType.Type &&
409+
TypesAreCompatible(apiResponseType?.Type, metadata?.Type) &&
409410
metadata.Description is not null)
410411
{
411412
matchingDescription = metadata.Description;
412413
}
413414
}
414415
return matchingDescription;
415416
}
417+
418+
static bool TypesAreCompatible(Type? apiResponseType, Type? metadaType)
419+
{
420+
// We need to a special check for cases where the inferred type is different than the one specified in attributes.
421+
// For example, an endpoint that defines [ProducesResponseType<IEnumerable<WeatherForecast>>],
422+
// but the endpoint returns weatherForecasts.ToList(). Because List<> is a different type than IEnumerable<>, it would incorrectly return false.
423+
// Currently, we do a "simple" biderectional check to see if the types are assignable to each other.
424+
// This isn't very thorough, but it works for most cases.
425+
// For more information, check the related bug: https://github.com/dotnet/aspnetcore/issues/60518
426+
return apiResponseType == metadaType ||
427+
metadaType?.IsAssignableFrom(apiResponseType) == true ||
428+
apiResponseType?.IsAssignableFrom(metadaType) == true;
429+
}
416430
}
417431

418432
private static ApiResponseType CreateDefaultApiResponseType(Type responseType)

0 commit comments

Comments
 (0)