.NET 9 OpenAPI document not generating at build time (Microsoft.Extensions.ApiDescription.Server)

Nick Diplos 0 Reputation points
2025-04-15T15:10:36.5866667+00:00

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 relevantdotnet-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.

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
420 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Nick Diplos 0 Reputation points
    2025-04-16T09:02:58.6433333+00:00

    I solved the problem by finding the document generated if I removed the following package:

    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.1.1" />

    If you're trying to generate OpenAPI documents at build time with Microsoft.Extensions.ApiDescription.Server, make sure you don’t have Swashbuckle.AspNetCore.SwaggerGen installed.

    In my case, having this package was silently preventing the build-time OpenAPI generation from working. Once I uninstalled Swashbuckle, everything worked perfectly and the OpenAPI file was generated during dotnet build as expected.

    0 comments No comments

  2. Bruce (SqlWork.com) 75,051 Reputation points
    2025-04-16T18:26:38.7766667+00:00

    the OpenApi package updates your project file. a build step is added to generate the json file in the obj folder, which is used as a resource by the binary.

    so be sure the the project has the build step:

      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
        <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.4">
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
          <PrivateAssets>all</PrivateAssets>
        </PackageReference>
      </ItemGroup>
    
    

    and program.cs added the OpenApi support (maps the route to the :

        app.MapOpenApi();
        // optional
        app.MapGet("/openapi", () => Results.Redirect("openapi/v1.json"));
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.