Minimal apis don't show up if GetSwaggerAsyc is called too early
Swashbuckle version 6.4 and 6.5 (didn't test others).
- Generate a new minimal api project
- add
await app.Services.GetRequiredService<IAsyncSwaggerProvider>().GetSwaggerAsync("v1");just beforeapp.Run()in program.cs - 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.
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.
Same problem happens to me when using <OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild> in my csproj.
Turns out, I could fix the issue by upgrading Microsoft.Extensions.ApiDescription.Server to the newest version.
Right now it seems like version 6.0.5 is referenced. Once I upgraded it to a 8.0.x version, it worked again in my .NET 8 project.
Microsoft.Extensions.ApiDescription.Server seems to be backwards compatible, so maybe it would be a good idea to bump the version of this reference in Swashbuckle.AspNetCore.
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.
bump
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.
This issue was closed because it has been inactive for 14 days since being marked as stale.