Skip to content

Commit 578990b

Browse files
committed
Add doc on generating multiple OpenAPI documents
1 parent c165b8e commit 578990b

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

aspnetcore/fundamentals/openapi/aspnetcore-openapi.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ dotnet add package Microsoft.AspNetCore.OpenApi
4545

4646
The following code:
4747

48-
* Adds OpenAPI services.
49-
* Enables the endpoint for viewing the OpenAPI document in JSON format.
48+
* Adds OpenAPI services using the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> extension method on the app builder's service collection.
49+
* Maps an endpoint for viewing the OpenAPI document in JSON format with the <xref:Microsoft.AspNetCore.Builder.OpenApiEndpointRouteBuilderExtensions.MapOpenApi%2A> extension method on the app.
5050

5151
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,7)]
5252

@@ -64,7 +64,7 @@ Each OpenAPI document in an app has a unique name. The default document name tha
6464
builder.Services.AddOpenApi(); // Document name is v1
6565
```
6666

67-
The document name can be modified by passing the name as a parameter to the `AddOpenApi` call.
67+
The document name can be modified by passing the name as a parameter to the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> call.
6868

6969
```csharp
7070
builder.Services.AddOpenApi("internal"); // Document name is internal
@@ -116,6 +116,36 @@ The OpenAPI document is regenerated every time a request to the OpenAPI endpoint
116116

117117
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_mapopenapiwithcaching)]
118118

119+
## Generate multiple OpenAPI documents
120+
121+
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:
122+
123+
* Generating OpenAPI documentation for different audiences, such as public and internal APIs.
124+
* Generating OpenAPI documentation for different versions of an API.
125+
* Generating OpenAPI documentation for different parts of an application, such as a frontend and backend API.
126+
127+
To generate multiple OpenAPI documents, call the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> extension method once for each document, specifying a different document name in the first parameter each time.
128+
129+
```csharp
130+
builder.Services.AddOpenApi("v1");
131+
builder.Services.AddOpenApi("v2");
132+
```
133+
134+
Each invocation of <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> can specify its own set of options, so that you can choose to use the same or different transformers / customizations for each OpenApi document.
135+
136+
The framework uses the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method of <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions> to determine which endpoints to include in each document.
137+
138+
For each document, the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method is called for each endpoint in the application, passing the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object for the endpoint. The method should return a boolean value indicating whether the endpoint should be included in the document. The <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object contains information about the endpoint, such as the HTTP method, route, and response types, as well as metadata attached to the endpoint via attributes or extension methods.
139+
140+
The default implementation of this delegate uses the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription.GroupName> field of <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription>, which is set on an endpoint using either the <xref:Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithGroupName%2A> extension method or the <xref:Microsoft.AspNetCore.Routing.EndpointGroupNameAttribute> attribute, to determine which endpoints to include in the document.
141+
142+
```csharp
143+
// Include endpoints without a group name or with a group name that matches the document name
144+
ShouldInclude = (description) => description.GroupName == null || description.GroupName == DocumentName;
145+
```
146+
147+
You can customize the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method to include or exclude endpoints based on any criteria you choose.
148+
119149
## Generate OpenAPI documents at build-time
120150

121151
In typical web applications, OpenAPI documents are generated at run-time and served via an HTTP request to the application server.

0 commit comments

Comments
 (0)