Skip to content

Commit 01b212e

Browse files
Rick andy/open api/m2-2 (#34808)
* Correct naming and output for build-time generated OpenAPI documents * clean up of OpenAPI * work * Update aspnetcore/fundamentals/openapi/aspnetcore-openapi.md --------- Co-authored-by: Thyge S. Steffensen <[email protected]>
1 parent 0f30e6c commit 01b212e

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

aspnetcore/fundamentals/openapi/aspnetcore-openapi.md

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to generate and customize OpenAPI documents in an ASP.NET
55
ms.author: safia
66
monikerRange: '>= aspnetcore-6.0'
77
ms.custom: mvc
8-
ms.date: 01/23/2025
8+
ms.date: 2/23/2025
99
uid: fundamentals/openapi/aspnetcore-openapi
1010
---
1111
# Generate OpenAPI documents
@@ -14,7 +14,7 @@ uid: fundamentals/openapi/aspnetcore-openapi
1414

1515
The [`Microsoft.AspNetCore.OpenApi`](https://www.nuget.org/packages/Microsoft.AspNetCore.OpenApi) package provides built-in support for OpenAPI document generation in ASP.NET Core. The package provides the following features:
1616

17-
* Support for generating OpenAPI documents at run time and accessing them via an endpoint on the application.
17+
* Support for generating OpenAPI documents at run time and accessing them via an endpoint on the app.
1818
* Support for "transformer" APIs that allow modifying the generated document.
1919
* Support for generating multiple OpenAPI documents from a single app.
2020
* Takes advantage of JSON schema support provided by [`System.Text.Json`](/dotnet/api/system.text.json).
@@ -112,7 +112,7 @@ The OpenAPI endpoint doesn't enable any authorization checks by default. Howeve
112112

113113
#### Cache generated OpenAPI document
114114

115-
The OpenAPI document is regenerated every time a request to the OpenAPI endpoint is sent. Regeneration enables transformers to incorporate dynamic application 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.
115+
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.
116116

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

@@ -122,7 +122,7 @@ In some scenarios, it's helpful to generate multiple OpenAPI documents with diff
122122

123123
* Generating OpenAPI documentation for different audiences, such as public and internal APIs.
124124
* 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.
125+
* Generating OpenAPI documentation for different parts of an app, such as a frontend and backend API.
126126

127127
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.
128128

@@ -135,9 +135,12 @@ Each invocation of <xref:Microsoft.Extensions.DependencyInjection.OpenApiService
135135

136136
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.
137137

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 returns 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.
138+
For each document, the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method is called for each endpoint in the app, passing the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object for the endpoint. The method returns a boolean value indicating whether the endpoint should be included in the document. The <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object:
139139

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. Any endpoint that has not been assigned a group name is included all OpenAPI documents.
140+
* contains information about the endpoint, such as the HTTP method, route, and response types
141+
* Metadata attached to the endpoint via attributes or extension methods.
142+
143+
The default implementation of this delegate uses the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription.GroupName> field of <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription>. The delegate 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. `WithGroupName` or the `EndpointGroupName` attribute determines which endpoints to include in the document. Any endpoint that has not been assigned a group name is included all OpenAPI documents.
141144

142145
```csharp
143146
// Include endpoints without a group name or with a group name that matches the document name
@@ -148,13 +151,13 @@ You can customize the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldIn
148151

149152
## Generate OpenAPI documents at build-time
150153

151-
In typical web applications, OpenAPI documents are generated at run-time and served via an HTTP request to the application server.
154+
In typical web apps, OpenAPI documents are generated at run-time and served via an HTTP request to the app server.
152155

153-
In some scenarios, it's helpful to generate the OpenAPI document during the application's build step. These scenarios include:
156+
In some scenarios, it's helpful to generate the OpenAPI document during the app's build step. These scenarios include:
154157

155-
- Generating OpenAPI documentation that is committed into source control.
156-
- Generating OpenAPI documentation that is used for spec-based integration testing.
157-
- Generating OpenAPI documentation that is served statically from the web server.
158+
* Generating OpenAPI documentation that is committed into source control.
159+
* Generating OpenAPI documentation that is used for spec-based integration testing.
160+
* Generating OpenAPI documentation that is served statically from the web server.
158161

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

@@ -173,20 +176,37 @@ Run the following command in the directory that contains the project file:
173176
```dotnetcli
174177
dotnet add package Microsoft.Extensions.ApiDescription.Server
175178
```
179+
176180
---
177181

178-
Upon installation, this package will automatically generate the Open API document(s) associated with the application during build and populate them into the application's output directory.
182+
Upon installation, this package:
183+
184+
* Automatically generates the Open API document(s) associated with the app during build.
185+
* Populates the Open API documents in the app's output directory.
186+
187+
If multiple documents are registered, ***and*** document name is ***not*** `v1`, it's post-fixed with the document name. E.g., `{ProjectName}_{DocumentName}.json`.
188+
189+
# [Visual Studio Code](#tab/visual-studio-code)
190+
191+
```powershell
192+
dotnet build
193+
type obj\{ProjectName}.json
194+
```
195+
196+
# [.NET Core CLI](#tab/netcore-cli)
179197

180198
```cli
181-
$ dotnet build
182-
$ cat bin/Debug/net9.0/{ProjectName}.json
199+
dotnet build
200+
cat obj/{ProjectName}.json
183201
```
184202

203+
---
204+
185205
### Customizing build-time document generation
186206

187207
#### Modifying the output directory of the generated Open API file
188208

189-
By default, the generated OpenAPI document will be emitted to the application's output directory. To modify the location of the emitted file, set the target path in the `OpenApiDocumentsDirectory` property.
209+
By default, the generated OpenAPI document will be emitted to the app's output directory. To modify the location of the emitted file, set the target path in the `OpenApiDocumentsDirectory` property.
190210

191211
```xml
192212
<PropertyGroup>
@@ -198,7 +218,7 @@ The value of `OpenApiDocumentsDirectory` is resolved relative to the project fil
198218

199219
#### Modifying the output file name
200220

201-
By default, the generated OpenAPI document will have the same name as the application's project file. To modify the name of the emitted file, set the `--file-name` argument in the `OpenApiGenerateDocumentsOptions` property.
221+
By default, the generated OpenAPI document will have 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.
202222

203223
```xml
204224
<PropertyGroup>
@@ -208,7 +228,7 @@ By default, the generated OpenAPI document will have the same name as the applic
208228

209229
#### Selecting the OpenAPI document to generate
210230

211-
Some applications may be configured to emit multiple OpenAPI documents, for various versions of an API or to distinguish between public and internal APIs. By default, the build-time document generator will emit files for all documents that are configured in an application. To only emit for a single document name, set the `--document-name` argument in the `OpenApiGenerateDocumentsOptions` property.
231+
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.
212232

213233
```xml
214234
<PropertyGroup>

0 commit comments

Comments
 (0)