-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FROZEN: Add build-time OpenAPI generation doc #33359
Draft
captainsafia
wants to merge
2
commits into
main
Choose a base branch
from
openapi-build-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
title: Generate OpenAPI documents at build time | ||
author: captainsafia | ||
description: Learn how to generate OpenAPI documents in your application's build step | ||
ms.author: safia | ||
monikerRange: '>= aspnetcore-6.0' | ||
ms.custom: mvc | ||
ms.date: 8/13/2024 | ||
uid: fundamentals/openapi/buildtime-openapi | ||
--- | ||
|
||
# Generate OpenAPI documents at build-time | ||
|
||
In a typical web applications, OpenAPI documents are generated at run-time and served via an HTTP request to the application server. | ||
|
||
In some scenarios, it is helpful to generate the OpenAPI document during the application's build step. These scenarios including: | ||
|
||
- Generating OpenAPI documentation that is committed into source control | ||
- Generating OpenAPI documentation that is used for spec-based integration testing | ||
- Generating OpenAPI documentation that is served statically from the web server | ||
|
||
To add support for generating OpenAPI documents at build time, install the `Microsoft.Extensions.ApiDescription.Server` package: | ||
|
||
### [Visual Studio](#tab/visual-studio) | ||
|
||
Run the following command from the **Package Manager Console**: | ||
|
||
```powershell | ||
Install-Package Microsoft.Extensions.ApiDescription.Server -IncludePrerelease | ||
``` | ||
|
||
### [.NET CLI](#tab/net-cli) | ||
|
||
Run the following command in the directory that contains the project file: | ||
|
||
```dotnetcli | ||
dotnet add package Microsoft.Extensions.ApiDescription.Server --prerelease | ||
``` | ||
--- | ||
|
||
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. | ||
|
||
```cli | ||
$ dotnet build | ||
$ cat bin/Debub/net9.0/{ProjectName}.json | ||
``` | ||
|
||
## Customizing build-time document generation | ||
|
||
### Modifying the output directory of the generated Open API file | ||
|
||
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. | ||
|
||
```xml | ||
<PropertyGroup> | ||
<OpenApiDocumentsDirectory>./</OpenApiDocumentsDirectory> | ||
</PropertyGroup> | ||
``` | ||
|
||
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. | ||
|
||
### Modifying the output file name | ||
|
||
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. | ||
|
||
```xml | ||
<PropertyGroup> | ||
<OpenApiGenerateDocumentsOptions>--file-name my-open-api</OpenApiGenerateDocumentsOptions> | ||
</PropertyGroup> | ||
``` | ||
|
||
### Selecting the OpenAPI document to generate | ||
|
||
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. | ||
|
||
```xml | ||
<PropertyGroup> | ||
<OpenApiGenerateDocumentsOptions>--document-name v2</OpenApiGenerateDocumentsOptions> | ||
</PropertyGroup> | ||
``` | ||
|
||
## Customizing run-time behavior during build-time document generation | ||
|
||
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: | ||
|
||
- Not reading from certain configuration strings | ||
- Not registering database-related services | ||
|
||
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: | ||
|
||
```csharp | ||
using System.Reflection; | ||
|
||
var builder = WebApplication.CreateBuilder(); | ||
|
||
if (Assembly.GetEntryAssembly()?.GetName().Name != "GetDocument.Insider") | ||
{ | ||
builders.Services.AddDefaults(); | ||
} | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/global.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"sdk": { | ||
"version": "9.0.100-rc.1.24414.13" | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/my-open-api.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"openapi": "3.0.1", | ||
"info": { | ||
"title": "GetDocument.Insider | v1", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"tags": [ | ||
"GetDocument.Insider" | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"content": { | ||
"text/plain": { | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { }, | ||
"tags": [ | ||
{ | ||
"name": "GetDocument.Insider" | ||
} | ||
] | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rick-Anderson @tdykstra This definitely needs some massaging for prose and formatting but the material covered is true to what I think we should cover. Feel free to modify this branch as you see fit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I merge #33361 into this branch? You've reviewed #33361 and @mikekistler has approved my changes. My PR fixes the toc.yml merge conflict. I think I can easily fix the other merge conflicts.