แก้ไข

Generate OpenAPI documents

The Microsoft.AspNetCore.OpenApi package provides built-in support for OpenAPI document generation in ASP.NET Core. The package provides the following features:

  • Support for generating OpenAPI version 3.1 documents starting with .NET 10, and OpenAPI version 3.2 documents starting with .NET 11.
  • Support for JSON Schema draft 2020-12.
  • Support for generating OpenAPI documents at run time and accessing them via an endpoint on the app.
  • Support for "transformer" APIs that allow modifying the generated document.
  • Support for generating multiple OpenAPI documents from a single app.
  • Takes advantage of JSON schema support provided by System.Text.Json.
  • Compatible with native AoT.

Starting in .NET 11, the default OpenAPI version for generated documents is 3.2. In .NET 10, the default is 3.1. To change the version, explicitly set the OpenApiVersion property of the OpenApiOptions in the configureOptions delegate parameter of AddOpenApi:

builder.Services.AddOpenApi(options =>
{
    // Specify the OpenAPI version to use
    options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
});

When generating the OpenAPI document at build time, the OpenAPI version can be selected by setting the --openapi-version in the OpenApiGenerateDocumentsOptions MSBuild item.

<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.1 document -->
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_1</OpenApiGenerateDocumentsOptions>

Package installation

Install the Microsoft.AspNetCore.OpenApi package:

Run the following command from the Package Manager Console:

Install-Package Microsoft.AspNetCore.OpenApi

Configure OpenAPI document generation

The following code:

  • Adds OpenAPI services using the AddOpenApi extension method on the app builder's service collection.
  • Maps an endpoint for viewing the OpenAPI document in JSON format with the MapOpenApi extension method on the app.
var builder = WebApplication.CreateBuilder();

builder.Services.AddOpenApi();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.MapGet("/", () => "Hello world!");

app.Run();

Launch the app and navigate to https://localhost:{PORT}/openapi/v1.json to view the generated OpenAPI document, where the {PORT} placeholder is the port.

OpenAPI document generation applies the following behaviors:

  • Unknown HTTP methods are excluded from the generated document. For example, the QUERY method is an HTTP method that isn't yet recognized by the OpenAPI specification, so it's gracefully excluded from the generated document.
  • Numbers and dates are formatted using the invariant culture, so the generated document is consistent regardless of the server's culture settings.
  • The QUERY method and app-defined custom HTTP methods are described in the generated document. For more information, see Operations for HTTP QUERY and custom HTTP methods.
  • Endpoints whose HTTP method is missing or isn't a syntactically valid HTTP method token are excluded from the generated document.
  • Numbers and dates are formatted using the invariant culture, so the generated document is consistent regardless of the server's culture settings.

Operations for HTTP QUERY and custom HTTP methods

Routing accepts arbitrary HTTP method strings through MapMethods. OpenAPI document generation describes those operations instead of omitting them.

HTTP QUERY is a proposed safe, idempotent method that lets a client send a request body when describing a search. It's useful when a query is too large or too structured to fit in a URL. OpenAPI 3.2 defines a query field on the Path Item Object for exactly this case.

The following endpoint uses MapMethods to handle QUERY requests:

app.MapMethods("/search", ["QUERY"], (SearchRequest request) =>
    SearchService.Run(request));

In an OpenAPI 3.2 document, the operation appears inline as a sibling of get, post, and the other standard operations:

"paths": {
  "/search": {
    "query": {
      "requestBody": { },
      "responses": { "200": { } }
    }
  }
}

An HTTP method other than QUERY that the OpenAPI specification doesn't define as a fixed field appears under the Path Item's additionalOperations field, keyed by the uppercased method name:

"paths": {
  "/purge": {
    "additionalOperations": {
      "PURGE": {
        "responses": { "200": { } }
      }
    }
  }
}

In an OpenAPI 3.0 or 3.1 document, neither field exists, so both QUERY and other custom methods are emitted under the x-oai-additionalOperations specification extension instead:

"paths": {
  "/search": {
    "x-oai-additionalOperations": {
      "QUERY": {
        "requestBody": { },
        "responses": { "200": { } }
      }
    }
  }
}

To generate a 3.0 or 3.1 document, set OpenApiVersion. For more information, see Customize the OpenAPI version of a generated document.

Note

An endpoint is described only when its HTTP method is a syntactically valid HTTP method token. An endpoint registered with a missing or malformed method string is skipped, and the rest of the document is generated normally.

Options to Customize OpenAPI document generation

The following sections demonstrate how to customize OpenAPI document generation.

Generate OpenAPI document in YAML format

The OpenAPI document can be generated in either JSON or YAML format. By default, the OpenAPI document is generated in JSON format. To generate the OpenAPI document in YAML format, specify the endpoint in the MapOpenApi call with a ".yaml" or ".yml" suffix, as shown in the following example, where the {documentName} placeholder is the document name:

app.MapOpenApi("/openapi/{documentName}.yaml");

Generating OpenAPI documents in YAML format at build time isn't supported but planned for a future preview.

Customize the OpenAPI document name

Each OpenAPI document in an app has a unique name. The default document name that is registered is v1:

builder.Services.AddOpenApi(); // Document name is v1

The document name can be modified by passing the name as a parameter to the AddOpenApi call:

builder.Services.AddOpenApi("internal"); // Document name is internal

The document name surfaces in several places in the OpenAPI implementation.

When fetching the generated OpenAPI document, the document name is provided as the documentName parameter argument in the request. The following requests resolve the v1 and internal documents.

GET http://localhost:5000/openapi/v1.json
GET http://localhost:5000/openapi/internal.json

Customize the OpenAPI version of a generated document

By default, OpenAPI document generation creates an OpenAPI 3.1 document. The following code demonstrates how to target a different version of the OpenAPI specification:

builder.Services.AddOpenApi(options =>
{
    options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_0;
});

By default, OpenAPI document generation creates an OpenAPI 3.2 document. Target an earlier version when downstream tooling doesn't yet handle OpenAPI 3.2. The following code generates an OpenAPI 3.1 document:

builder.Services.AddOpenApi(options =>
{
    options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1;
});

For more information, see OpenApiVersion defaults to OpenApi3_2.

Customize the OpenAPI endpoint route

By default, the OpenAPI endpoint registered via a call to MapOpenApi exposes the document at the /openapi/{documentName}.json endpoint. The following code demonstrates how to customize the route at which the OpenAPI document is registered:

app.MapOpenApi("/openapi/{documentName}/openapi.json");

It's possible, but not recommended, to remove the documentName route parameter from the endpoint route. When the documentName route parameter is removed from the endpoint route, the framework attempts to resolve the document name from the query parameter. Not providing the documentName in either the route or query can result in unexpected behavior.

Customize the OpenAPI endpoint

Because the OpenAPI document is served via a route handler endpoint, any customization that is available to standard minimal endpoints is available to the OpenAPI endpoint.

Limit OpenAPI document access to authorized users

The OpenAPI endpoint doesn't enable authorization checks by default. However, authorization checks can be applied to the OpenAPI document. In the following code, access to the OpenAPI document is limited to users with the tester role:

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder();

builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddAuthorization(o =>
{
    o.AddPolicy("ApiTesterPolicy", b => b.RequireRole("tester"));
});
builder.Services.AddOpenApi();

var app = builder.Build();

app.MapOpenApi()
    .RequireAuthorization("ApiTesterPolicy");

app.MapGet("/", () => "Hello world!");

app.Run();

Cache generated OpenAPI document

The OpenAPI document is regenerated every time a request to the OpenAPI endpoint is sent. Regeneration enables transformers to incorporate dynamic app state into their operation. For example, regenerating a request with details of the HTTP context. When applicable, the OpenAPI document can be cached to avoid executing the document generation pipeline on each HTTP request.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder();

builder.Services.AddOutputCache(options =>
{
    options.AddBasePolicy(policy => policy.Expire(TimeSpan.FromMinutes(10)));
});
builder.Services.AddOpenApi();

var app = builder.Build();

app.UseOutputCache();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi()
        .CacheOutput();
}

app.MapGet("/", () => "Hello world!");

app.Run();

Generate multiple OpenAPI documents

In some scenarios, it's helpful to generate multiple OpenAPI documents with different content from a single ASP.NET Core API app. These scenarios include generating OpenAPI documentation for different:

  • Audiences, such as public and internal APIs.
  • Versions of an API.
  • Parts of an app, such as a frontend and backend API.

To generate multiple OpenAPI documents, call the AddOpenApi extension method once for each document, specifying a different document name in the first parameter each time.

builder.Services.AddOpenApi("v1");
builder.Services.AddOpenApi("v2");

Each invocation of AddOpenApi can specify its own set of options, so you can choose to use the same or different customizations for each OpenAPI document.

The framework uses the ShouldInclude delegate method of OpenApiOptions to determine which endpoints to include in each document.

For each document, the ShouldInclude delegate method is called for each endpoint in the app, passing the ApiDescription object for the endpoint. The method returns a boolean value indicating whether the endpoint should be included in the document. The ApiDescription object:

  • Contains information about the endpoint, such as the HTTP method, route, and response types.
  • Metadata attached to the endpoint via attributes or extension methods.

The default implementation of this delegate uses the GroupName field of ApiDescription. The delegate is set on an endpoint using either the WithGroupName extension method or the EndpointGroupNameAttribute attribute. WithGroupName or the EndpointGroupName attribute determines which endpoints to include in the document. Any endpoint that hasn't been assigned a group name is included in all OpenAPI documents.

// Include endpoints without a group name or with a group name
// that matches the document name
ShouldInclude = (description) => description.GroupName == null || 
    description.GroupName == DocumentName;    

You can customize the ShouldInclude delegate method to include or exclude endpoints based on any criteria.

Generate OpenAPI documents at build time

Note

Starting with .NET 9, ASP.NET Core includes built-in OpenAPI support. The Microsoft.AspNetCore.OpenApi package provides OpenAPI document generation at runtime, and adding the Microsoft.Extensions.ApiDescription.Server package enables build-time document generation.

ASP.NET Core generates OpenAPI documents only. Interactive UIs such as Swagger UI or Scalar are not included by default and must be added separately. For guidance on using these UI options, see Use the generated OpenAPI documents.

In typical web apps, OpenAPI documents are generated at runtime and served via an HTTP request to the app server.

In some scenarios, it's helpful to generate the OpenAPI document during the app's build step. These scenarios include generating OpenAPI documentation that is:

  • Committed into source control.
  • Used for spec-based integration testing.
  • Served statically from the web server.

To add support for generating OpenAPI documents at build time, install the Microsoft.Extensions.ApiDescription.Server package:

Run the following command from the Package Manager Console:

Install-Package Microsoft.Extensions.ApiDescription.Server

Upon installation, this package:

  • Automatically generates the Open API documents associated with the app during build.
  • Populates the Open API documents in the app's output directory.

If multiple documents are registered and the document name is not v1, the project name is post-fixed with the document name. Example: {ProjectName}_{DocumentName}.json. The {ProjectName} placeholder is the project name, and the {DocumentName} placeholder is the document name.

dotnet build
type obj\{ProjectName}.json

View build-time OpenAPI logs (Terminal Logger)

When Microsoft.Extensions.ApiDescription.Server runs the GetDocument step during dotnet build, progress messages aren't visible with the .NET Terminal Logger at default verbosity in .NET 8 or later. To surface these messages while building, use either of the following options with the dotnet build command.

Set the Terminal Logger's verbosity with the -tlp option set to v=d (verbosity = detailed):

dotnet build -tlp:v=d

Disable the Terminal Logger and use legacy-style logs with the --tl option set to off:

dotnet build --tl:off

Customize build-time document generation

Modify the output directory of the generated Open API file

By default, the generated OpenAPI document is emitted to the app's output directory. To modify the location of the emitted file, set the target path in the OpenApiDocumentsDirectory property:

<PropertyGroup>
  <OpenApiDocumentsDirectory>.</OpenApiDocumentsDirectory>
</PropertyGroup>

The value of OpenApiDocumentsDirectory is resolved relative to the project file. Using the . value, as seen in the preceding example, emits the OpenAPI document in the same directory as the project file.

Modify the output file name

By default, the generated OpenAPI document has the same name as the app's project file. To modify the name of the emitted file, set the --file-name argument in the OpenApiGenerateDocumentsOptions property:

<PropertyGroup>
  <OpenApiGenerateDocumentsOptions>--file-name my-open-api</OpenApiGenerateDocumentsOptions>
</PropertyGroup>

Select the OpenAPI document to generate

Some apps may be configured to emit multiple OpenAPI documents. Multiple OpenAPI documents may be generated for different versions of an API or to distinguish between public and internal APIs. By default, the build-time document generator emits files for all documents that are configured in an app. To only emit for a single document name, set the --document-name argument in the OpenApiGenerateDocumentsOptions property:

<PropertyGroup>
  <OpenApiGenerateDocumentsOptions>--document-name v2</OpenApiGenerateDocumentsOptions>
</PropertyGroup>

Select the app environment

Starting in .NET 11, set the OpenApiGenerationEnvironment property to select the app environment used during build-time OpenAPI document generation. The property sets the host's environment for the generation process, equivalent to setting the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable:

<PropertyGroup>
  <OpenApiGenerationEnvironment>Development</OpenApiGenerationEnvironment>
</PropertyGroup>

Selecting the environment enables environment-specific configuration, such as settings from appsettings.Development.json, and environment-dependent document transformations to affect the generated document. The property doesn't change the environment used when the app runs normally.

Customize runtime behavior during build-time document generation

Build-time OpenAPI document generation functions by launching the apps entrypoint with a mock server implementation. A mock server is required to produce accurate OpenAPI documents because all information in the OpenAPI document can't be statically analyzed. Because the apps entrypoint is invoked, any logic in the apps startup is invoked. This includes code that injects services into the DI container or reads from configuration. In some scenarios, it's necessary to restrict the code paths when the app's entry point is invoked from build-time document generation. These scenarios include:

  • Not reading from certain configuration strings.
  • Not registering database-related services.

In order to restrict invoking these code paths by the build-time generation pipeline, they can be conditioned behind a check of the entry assembly:

using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

if (Assembly.GetEntryAssembly()?.GetName().Name != "GetDocument.Insider")
{
    builder.AddServiceDefaults();
}

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    app.UseHsts();
}

var myKeyValue = app.Configuration["MyKey"];

app.MapGet("/", () => {
    return Results.Ok($"The value of MyKey is: {myKeyValue}");
})
.WithName("TestKey");

app.Run();

AddServiceDefaults adds common Aspire services such as service discovery, resilience, health checks, and OpenTelemetry.

Trimming and Native AOT

OpenAPI in ASP.NET Core supports trimming and native AOT. The following steps create and publish an OpenAPI app with trimming and native AOT.

Create a new ASP.NET Core Web API (Native AOT) project:

dotnet new webapiaot

Publish the app:

dotnet publish