Hi all,
I'm working on a .NET 9 web API project and I'm trying to generate an OpenAPI document at build time, as described in the official documentation here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/aspnetcore-openapi?view=aspnetcore-9.0#generate-openapi-documents-at-build-time
Despite following all the documented steps, the OpenAPI file is not being generated during build. Here's a detailed breakdown:
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.4" />
I have added the following to my csproj file, but I have also tried without:
<PropertyGroup>
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
<OpenApiGenerateDocumentsOptions>--file-name openapi</OpenApiGenerateDocumentsOptions>
<OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)\openapi</OpenApiDocumentsDirectory>
</PropertyGroup>
What I've tried:
- Cleaned and rebuilt the project several times
- Verified
Microsoft.Extensions.ApiDescription.Server
appears in project.assets.json
- Checked
obj
for output like in the docs and it's not there.
- No reference to OpenApi appears in bear logs (or the relevant
dotnet-getdocument.dll)
I have another project (similar setup) where OpenAPI doc is generated at build. The main difference between these projects is that my current one using minimal API whereas the first is using controllers
Here are relevant parts of my Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyApi", Version = "v1" });
});
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapGet("/example/current-time", () =>
{
var currentTime = new
{
TimeLocal = DateTime.Now,
TimeZone = TimeZoneInfo.Local.DisplayName
};
return currentTime;
}).WithOpenApi();
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API Example"));
app.Run();
Note: While I have added swagger back into the application while trying to resolve the issue, I was actually hoping to us the built-in support for OpenApi through the Microsoft.AspNetCore.OpenApi package.