Skip to content

Commit 77e48fe

Browse files
committed
Change type check to a one-way approach
1 parent 46437fd commit 77e48fe

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,14 @@ static bool TypesAreCompatible(Type? apiResponseType, Type? metadataType)
418418
{
419419
// We need to a special check for cases where the inferred type is different than the one specified in attributes.
420420
// For example, an endpoint that defines [ProducesResponseType<IEnumerable<WeatherForecast>>],
421-
// but the endpoint returns weatherForecasts.ToList(). Because List<> is a different type than IEnumerable<>, it would incorrectly return false.
422-
// Currently, we do a "simple" bidirectional check to see if the types are assignable to each other.
423-
// This isn't very thorough, but it works for most cases.
421+
// but the endpoint returns weatherForecasts.ToList(). Because List<> is a different type than IEnumerable<>, it would incorrectly set OpenAPI metadata incorrectly.
422+
// We use a conservative unidirectional check where the attribute type must be assignable from the inferred type.
423+
// This handles inheritance (BaseClass ← DerivedClass) and interface implementation (IEnumerable<T> ← List<T>).
424+
// This should be sufficient, as it's more common to specify an interface or base class type in the attribute and a concrete type in the endpoint implementation,
425+
// compared to doing the opposite.
424426
// For more information, check the related bug: https://github.com/dotnet/aspnetcore/issues/60518
425427
return apiResponseType == metadataType ||
426-
metadataType?.IsAssignableFrom(apiResponseType) == true ||
427-
apiResponseType?.IsAssignableFrom(metadataType) == true;
428+
metadataType?.IsAssignableFrom(apiResponseType) == true;
428429
}
429430
}
430431

0 commit comments

Comments
 (0)