You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// We need to a special check for cases where the inferred type is different than the one specified in attributes.
420
+
// 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 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.
426
+
// For more information, check the related bug: https://github.com/dotnet/aspnetcore/issues/60518
Assert.Equal(typeof(List<TimeSpan>),okResponseType.Type);// We use List as the inferred type has higher priority than those set by metadata (attributes)
Assert.Equal(typeof(List<TimeSpan>),okResponseType.Type);// We use List as the inferred type has higher priority than those set by metadata (attributes)
/// EndpointMetadataApiDescriptionProvider performs a one way type check for discovering response types to match the description that's set in [ProducesResponseType]
412
+
/// The reason we do a one-way check instead of a bidirectional check is to prevent too many positive matches.
413
+
/// </summary>
414
+
/// <remarks>
415
+
/// Example: If we did a bidirectional check, we would match something scenarios like this, which can cause confusion:
416
+
/// [ProducesResponseType<string>(StatusCodes.Status200OK, Description = "Returned with a string")] -> TypedResults.Ok(new object())
417
+
/// This would match because object is assignable to string,
418
+
/// but it doesn't make sense to add the Description to the object type because the attribute says we should return a string.
419
+
///
420
+
/// This test documents this desired behavior and will fail if the behavior changes, so the developer can double check if their change is intentional.
0 commit comments