|
| 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 | +``` |
0 commit comments