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

Code before:
        Assert.Equal(expectedOkDescription, okResponseType.Description);

        var createdOkFormat = Assert.Single(okResponseType.ApiResponseFormats);
        Assert.Equal("application/json", createdOkFormat.MediaType);
    }

    [Fact]
    public void AddsResponseDescription_WorksWithCollectionsWhereInnerTypeIsInEndpointAndIsBaseClass()
    {
        const string expectedOkDescription = "The weather forecast for the next 5 days.";

        var apiDescription = GetApiDescription([ProducesResponseType<List<GenericClass<string>>>(StatusCodes.Status200OK, Description = expectedOkDescription)]
        () => new List<BaseClass> { new() }.AsEnumerable());

        var okResponseType = Assert.Single(apiDescription.SupportedResponseTypes);

        Assert.Equal(200, okResponseType.StatusCode);
        Assert.Equal(typeof(IEnumerable<BaseClass>), okResponseType.Type); // We use IEnumerable<BaseClass> as the inferred type has higher priority than those set by metadata (attributes)
        Assert.Equal(typeof(IEnumerable<BaseClass>), okResponseType.ModelMetadata?.ModelType);
        Assert.Equal(expectedOkDescription, okResponseType.Description);

        var createdOkFormat = Assert.Single(okResponseType.ApiResponseFormats);
        Assert.Equal("application/json", createdOkFormat.MediaType);
    }

    [Fact]
    public void AddsResponseDescription_WorksWithCollectionsAndTypedResultsWhereInnerTypeIsInEndpointAndIsBaseClass()
    {
        const string expectedOkDescription = "The weather forecast for the next 5 days.";

        var apiDescription = GetApiDescription([ProducesResponseType<List<GenericClass<string>>>(StatusCodes.Status200OK, Description = expectedOkDescription)]
        () => TypedResults.Ok(new List<BaseClass> { new() }.AsEnumerable()));

        var okResponseType = Assert.Single(apiDescription.SupportedResponseTypes);

        Assert.Equal(200, okResponseType.StatusCode);
        Assert.Equal(typeof(IEnumerable<BaseClass>), okResponseType.Type); // We use IEnumerable<BaseClass> as the inferred type has higher priority than those set by metadata (attributes)
        Assert.Equal(typeof(IEnumerable<BaseClass>), okResponseType.ModelMetadata?.ModelType);
        Assert.Equal(expectedOkDescription, okResponseType.Description);

        var createdOkFormat = Assert.Single(okResponseType.ApiResponseFormats);
        Assert.Equal("application/json", createdOkFormat.MediaType);
    }

    [Fact]
    public void WithEmptyMethodBody_AddsResponseDescription()
    {
        const string expectedCreatedDescription = "A new item was created";
        const string expectedBadRequestDescription = "Validation failed for the request";

        var apiDescription = GetApiDescription(

Review comment (suggestion):
Do we have a test that documents the behavior of something like:

```csharp
[ProducesResponseType(typeof(UserDto), 200)]
Get() => TypedResults.Ok(new UserDto());

``` 

Code after:
        var apiDescription = GetApiDescription(
    [ProducesResponseType(typeof(TimeSpan), StatusCodes.Status201Created, Description = expectedCreatedDescription)]
        [ProducesResponseType(StatusCodes.Status400BadRequest, Description = expectedBadRequestDescription)]
        () => new InferredJsonClass());

        Assert.Equal(3, apiDescription.SupportedResponseTypes.Count);

        var rdfInferredResponseType = apiDescription.SupportedResponseTypes[0];

        Assert.Equal(200, rdfInferredResponseType.StatusCode);
        Assert.Equal(typeof(InferredJsonClass), rdfInferredResponseType.Type);
        Assert.Equal(typeof(InferredJsonClass), rdfInferredResponseType.ModelMetadata?.ModelType);

        var rdfInferredResponseFormat = Assert.Single(rdfInferredResponseType.ApiResponseFormats);
        Assert.Equal("application/json", rdfInferredResponseFormat.MediaType);
        Assert.Null(rdfInferredResponseType.Description); // There is no description set for the default "200" code, so we expect it to be null.

        var createdResponseType = apiDescription.SupportedResponseTypes[1];

        Assert.Equal(201, createdResponseType.StatusCode);
        Assert.Equal(typeof(TimeSpan), createdResponseType.Type);
        Assert.Equal(typeof(TimeSpan), createdResponseType.ModelMetadata?.ModelType);
        Assert.Equal(expectedCreatedDescription, createdResponseType.Description);

        var createdResponseFormat = Assert.Single(createdResponseType.ApiResponseFormats);
        Assert.Equal("application/json", createdResponseFormat.MediaType);

        var badRequestResponseType = apiDescription.SupportedResponseTypes[2];

        Assert.Equal(400, badRequestResponseType.StatusCode);
        Assert.Equal(typeof(InferredJsonClass), badRequestResponseType.Type);
        Assert.Equal(typeof(InferredJsonClass), badRequestResponseType.ModelMetadata?.ModelType);
        Assert.Equal(expectedBadRequestDescription, badRequestResponseType.Description);

        var badRequestResponseFormat = Assert.Single(badRequestResponseType.ApiResponseFormats);
        Assert.Equal("application/json", badRequestResponseFormat.MediaType);
    }

    /// <summary>
    /// Setting the description grabs the LAST description.
    // To validate this, we add multiple ProducesResponseType to validate that it only grabs the LAST ONE.
    /// </summary>
    [Fact]
    public void AddsResponseDescription_UsesLastOne()
    {
        const string expectedCreatedDescription = "A new item was created";
        const string expectedBadRequestDescription = "Validation failed for the request";

        var apiDescription = GetApiDescription(
    [ProducesResponseType(typeof(int), StatusCodes.Status201Created, Description = "First description")] // The first item is an int, not a timespan, shouldn't match
        [ProducesResponseType(typeof(int), StatusCodes.Status201Created, Description = "Second description")] // Not a timespan AND not the final item, shouldn't match