Skip to content

ApiExplorer doesn't propagate non-success Results types #65371

@klinki

Description

@klinki

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I originally submitted this bug report to domaindrivendev/Swashbuckle.AspNetCore#3775 but they pointed out that this is a bug in ApiExporter.

I tried to use Results<Ok<WeatherForecast[]>, UnauthorizedHttpResult, ProblemHttpResult> as controller response type. It seems ApiExporter exports only a success response type and doesn't contain description for non-success responses.

I expected it to contain at least 401 for the UnauthorizedHttpResult in responses object.

Expected Behavior

All types from Results should be included as possible response types.

Steps To Reproduce

C# Code
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.HttpResults;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapControllers();

app.Run();

public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    [HttpGet]
    public Results<Ok<WeatherForecast[]>, UnauthorizedHttpResult, ProblemHttpResult> Get()
    {
        try
        {
            var result = Random.Shared.Next(3);
            if (result == 1)
            {
                return TypedResults.Unauthorized();
            }

            if (result == 2)
            {
                throw new InvalidOperationException("Failed to get the weather forecast");
            }

            var forecast = Enumerable.Range(1, 5).Select(index =>
                new WeatherForecast
                (
                    DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                    Random.Shared.Next(-20, 55),
                    Summaries[Random.Shared.Next(Summaries.Length)]
                ))
                .ToArray();

            return TypedResults.Ok(forecast);
        }
        catch (Exception ex)
        {
            return TypedResults.Problem(ex.Message);
        }
    }
}

Exceptions (if any)

No response

.NET Version

10.0.102

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions