Repository: dotnet/aspnetcore
File: src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs (C#)

Code before:
            {
                // In some context, a typeof(void) return means that no response type was specified by the metadata. This can happen
                // if a user applied a [ProducesResponseType] attribute without a default type parameter. In this case, we should use the
                // response type inferred from the return type of the handler. For minimal API scenarios, where `typeof(void)` can be inferred
                // by the framework for handlers that return awaitables, we will only treat `typeof(void)` as a null type that should fall back to the
                // inference logic if it has been set as the default error type to retain back-compat.
                if (apiResponseType.Type is null || (apiResponseType.Type == typeof(void) && errorSetByDefault))
                {
                    apiResponseType.Type = responseType;
                }

                apiResponseType.ModelMetadata = CreateModelMetadata(apiResponseType.Type);

                if (contentTypes.Count > 0)
                {
                    AddResponseContentTypes(apiResponseType.ApiResponseFormats, contentTypes);
                }
                // Only set the default response type if it hasn't already been set via a
                // ProducesResponseTypeAttribute.
                else if (apiResponseType.ApiResponseFormats.Count == 0 && CreateDefaultApiResponseFormat(apiResponseType.Type) is { } defaultResponseFormat)
                {
                    apiResponseType.ApiResponseFormats.Add(defaultResponseFormat);
                }

                // We set the Description to the first non-null value we find that matches the status code.
                apiResponseType.Description ??= responseMetadataTypes.FirstOrDefault(x => x.StatusCode == apiResponseType.StatusCode && x.Description is not null)?.Description;

                if (!supportedResponseTypes.Any(existingResponseType => existingResponseType.StatusCode == apiResponseType.StatusCode))
                {
                    supportedResponseTypes.Add(apiResponseType);
                }
            }
        }
        else
        {
            // Set the default response type only when none has already been set explicitly with metadata.
            var defaultApiResponseType = CreateDefaultApiResponseType(responseType);

            if (contentTypes.Count > 0)
            {
                // If metadata provided us with response formats, use that instead of the default.
                defaultApiResponseType.ApiResponseFormats.Clear();
                AddResponseContentTypes(defaultApiResponseType.ApiResponseFormats, contentTypes);
            }

            supportedResponseTypes.Add(defaultApiResponseType);
        }
    }

    private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
    {

Review comment (suggestion):
Is there a reason the existing logic in the ApiResponseTypeProvider doesn't handle this? That feels like the right place to do this instead of having to filter on the types returned by it to set the description.


Code after:
            {
                // In some context, a typeof(void) return means that no response type was specified by the metadata. This can happen
                // if a user applied a [ProducesResponseType] attribute without a default type parameter. In this case, we should use the
                // response type inferred from the return type of the handler. For minimal API scenarios, where `typeof(void)` can be inferred
                // by the framework for handlers that return awaitables, we will only treat `typeof(void)` as a null type that should fall back to the
                // inference logic if it has been set as the default error type to retain back-compat.
                if (apiResponseType.Type is null || (apiResponseType.Type == typeof(void) && errorSetByDefault))
                {
                    apiResponseType.Type = responseType;
                }

                apiResponseType.ModelMetadata = CreateModelMetadata(apiResponseType.Type);

                if (contentTypes.Count > 0)
                {
                    AddResponseContentTypes(apiResponseType.ApiResponseFormats, contentTypes);
                }
                // Only set the default response type if it hasn't already been set via a
                // ProducesResponseTypeAttribute.
                else if (apiResponseType.ApiResponseFormats.Count == 0 && CreateDefaultApiResponseFormat(apiResponseType.Type) is { } defaultResponseFormat)
                {
                    apiResponseType.ApiResponseFormats.Add(defaultResponseFormat);
                }

                apiResponseType.Description ??= GetMatchingResponseTypeDescription(responseProviderMetadataTypes.Values, apiResponseType);

                if (!supportedResponseTypes.Any(existingResponseType => existingResponseType.StatusCode == apiResponseType.StatusCode))
                {
                    supportedResponseTypes.Add(apiResponseType);
                }
            }
        }
        else
        {
            // Set the default response type only when none has already been set explicitly with metadata.
            var defaultApiResponseType = CreateDefaultApiResponseType(responseType);

            if (contentTypes.Count > 0)
            {
                // If metadata provided us with response formats, use that instead of the default.
                defaultApiResponseType.ApiResponseFormats.Clear();
                AddResponseContentTypes(defaultApiResponseType.ApiResponseFormats, contentTypes);
            }

            supportedResponseTypes.Add(defaultApiResponseType);
        }

        static string? GetMatchingResponseTypeDescription(IEnumerable<ApiResponseType> responseMetadataTypes, ApiResponseType apiResponseType)
        {
            // We set the Description to the LAST non-null value we find that matches the status code.
            string? matchingDescription = null;