Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal apis don't show up if GetSwaggerAsyc is called too early #2774

Open
hahn-kev opened this issue Mar 20, 2024 · 1 comment
Open

Minimal apis don't show up if GetSwaggerAsyc is called too early #2774

hahn-kev opened this issue Mar 20, 2024 · 1 comment

Comments

@hahn-kev
Copy link

hahn-kev commented Mar 20, 2024

Swashbuckle version 6.4 and 6.5 (didn't test others).

  1. Generate a new minimal api project
  2. add await app.Services.GetRequiredService<IAsyncSwaggerProvider>().GetSwaggerAsync("v1"); just before app.Run() in program.cs
  3. Weather api does not show up in swagger

now the default weather api doesn't show up in swagger. Here's a full copy of the source:

using Swashbuckle.AspNetCore.Swagger;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.UseRouting();
app.MapGet("/weatherforecast",
        () =>
        {
            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 forecast;
        })
    .WithName("GetWeatherForecast")
    .WithOpenApi();
await app.Services.GetRequiredService<IAsyncSwaggerProvider>().GetSwaggerAsync("v1");
app.Run();

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

it seems like there's a race condition with calling GetSwaggerAsync too early where the minimal apis don't show up. MVC Controllers will show up correctly. The reason I'm doing this is to validate that we can generate a swagger doc on startup when devs are working. In my case I put it in a service and called generate after a 5 second delay to work around the issue.

@martincostello
Copy link
Collaborator

The reason I'm doing this is to validate that we can generate a swagger doc

If it were me, I'd use an integration test to validate this.

I wonder if the endpoints are lazily initialized on first use (or after Run()), so we can't "see" them yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants