Skip to content

Commit 2f05a5a

Browse files
committed
Add build-time OpenAPI generation docs
1 parent 8fda301 commit 2f05a5a

File tree

13 files changed

+148
-5
lines changed

13 files changed

+148
-5
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: Generate OpenAPI documents at build time
3+
author: captainsafia
4+
description: Learn how to generate OpenAPI documents in your application's build step
5+
ms.author: safia
6+
monikerRange: '>= aspnetcore-6.0'
7+
ms.custom: mvc
8+
ms.date: 8/13/2024
9+
uid: fundamentals/openapi/buildtime-openapi
10+
---
11+
12+
# Generate OpenAPI documents at build-time
13+
14+
In a typical web applications, OpenAPI documents are generated at run-time and served via an HTTP request to the application server.
15+
16+
In some scenarios, it is helpful to generate the OpenAPI document during the application's build step. These scenarios including:
17+
18+
- Generating OpenAPI documentation that is committed into source control
19+
- Generating OpenAPI documentation that is used for spec-based integration testing
20+
- Generating OpenAPI documentation that is served statically from the web server
21+
22+
To add support for generating OpenAPI documents at build time, install the `Microsoft.Extensions.ApiDescription.Server` package:
23+
24+
### [Visual Studio](#tab/visual-studio)
25+
26+
Run the following command from the **Package Manager Console**:
27+
28+
```powershell
29+
Install-Package Microsoft.Extensions.ApiDescription.Server -IncludePrerelease
30+
```
31+
32+
### [.NET CLI](#tab/net-cli)
33+
34+
Run the following command in the directory that contains the project file:
35+
36+
```dotnetcli
37+
dotnet add package Microsoft.Extensions.ApiDescription.Server --prerelease
38+
```
39+
---
40+
41+
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.
42+
43+
```cli
44+
$ dotnet build
45+
$ cat bin/Debub/net9.0/{ProjectName}.json
46+
```
47+
48+
## Customizing build-time document generation
49+
50+
### Modifying the output directory of the generated Open API file
51+
52+
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.
53+
54+
```xml
55+
<PropertyGroup>
56+
<OpenApiDocumentsDirectory>./</OpenApiDocumentsDirectory>
57+
</PropertyGroup>
58+
```
59+
60+
The value of `OpenApiDocumentsDirectory` is resolved relative to the project file. Using the `./` value above will emit the OpenAPI document in the same directory as the project file.
61+
62+
### Modifying the output file name
63+
64+
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.
65+
66+
```xml
67+
<PropertyGroup>
68+
<OpenApiGenerateDocumentsOptions>--file-name my-open-api</OpenApiGenerateDocumentsOptions>
69+
</PropertyGroup>
70+
```
71+
72+
### Selecting the OpenAPI document to generate
73+
74+
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.
75+
76+
```xml
77+
<PropertyGroup>
78+
<OpenApiGenerateDocumentsOptions>--document-name v2</OpenApiGenerateDocumentsOptions>
79+
</PropertyGroup>
80+
```
81+
82+
## Customizing run-time behavior during build-time document generation
83+
84+
Under the hood, build-time OpenAPI document generation functions by launching the application's entrypoint with an inert server implementation. This is a requirement to produce accurate OpenAPI documents since all information in the OpenAPI document cannot be statically analyzed. Because the application's entrypoint is invoked, any logic in the applications' startup will be invoked. This includes code that injects services into the DI container or reads from configuration. In some scenarios, it's necessary to restrict the codepaths that will run when the application's entry point is being invoked from build-time document generation. These scenarios include:
85+
86+
- Not reading from certain configuration strings
87+
- Not registering database-related services
88+
89+
In order to restrict these codepaths from being invoked by the build-time generation pipeline, they can be conditioned behind a check of the entry assembly like so:
90+
91+
```csharp
92+
using System.Reflection;
93+
94+
var builder = WebApplication.CreateBuilder();
95+
96+
if (Assembly.GetEntryAssembly()?.GetName().Name != "GetDocument.Insider")
97+
{
98+
builders.Services.AddDefaults();
99+
}
100+
```
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<OpenApiDocumentsDirectory>./</OpenApiDocumentsDirectory>
8+
<OpenApiGenerateDocumentsOptions>--file-name my-open-api</OpenApiGenerateDocumentsOptions>
79
</PropertyGroup>
810

911
<PropertyGroup>
@@ -19,7 +21,7 @@
1921
<PrivateAssets>all</PrivateAssets>
2022
</PackageReference>
2123
<PackageReference Include="Scalar.AspNetCore" Version="1.0.1" />
22-
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="6.5.0" />
24+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="6.7.0" />
2325
</ItemGroup>
2426

2527
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "9.0.100-rc.1.24414.13"
4+
}
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "GetDocument.Insider | v1",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/": {
9+
"get": {
10+
"tags": [
11+
"GetDocument.Insider"
12+
],
13+
"responses": {
14+
"200": {
15+
"description": "OK",
16+
"content": {
17+
"text/plain": {
18+
"schema": {
19+
"type": "string"
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
},
28+
"components": { },
29+
"tags": [
30+
{
31+
"name": "GetDocument.Insider"
32+
}
33+
]
34+
}

0 commit comments

Comments
 (0)