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 LAST non-null value we find that matches the status code.
                apiResponseType.Description ??= responseMetadataTypes.LastOrDefault(x => x.StatusCode == apiResponseType.StatusCode && x.Type == apiResponseType.Type && 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):
OK! I think I've grokked what is going on here. The handling is similar to what we do for `AddResponseContentTypes` above except that the implementation only picks the last match.

@sander1095 Thoughts on making this a bit obvious by creating a separate `ApplyDescription` local method that is implemented something like:

```csharp
private static string? GetMatchingResponseTypeDescription(IEnumerable<ApiResponseType> responseMetadataTypes, ApiResponseType apiResponseType)
{
    string? matchingDescription = null;
    foreach (var metadata in responseMetadataTypes)
    {
        if (metadata.StatusCode == apiResponseType.StatusCode && 
            metadata.Type == apiResponseType.Type && 
            metadata.Description is not null)
        {
            matchingDescription = metadata.Description;
        }
    }
    return matchingDescription;
}
 ```

The removal of the Linq helps make it a little bit easier to understand.



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;