From 90ee5ed55224f824cdcc8234dd5e117b8e2e1c6a Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Wed, 14 Aug 2024 13:06:29 -0700 Subject: [PATCH] Add build-time OpenAPI generation docs --- .../openapi/aspnetcore-openapi.md | 2 +- .../fundamentals/openapi/buildtime-openapi.md | 100 ++++++++++++++++++ .../samples/9.x}/WebMinOpenApi/.spectral.yaml | 0 .../samples/9.x}/WebMinOpenApi/Program.cs | 20 ++-- .../9.x}/WebMinOpenApi/WebMinOpenApi.csproj | 10 +- .../9.x}/WebMinOpenApi/WebMinOpenApi.json | 4 +- .../appsettings.Development.json | 0 .../9.x}/WebMinOpenApi/appsettings.json | 0 .../samples/9.x/WebMinOpenApi/global.json | 5 + .../9.x/WebMinOpenApi/my-open-api.json | 34 ++++++ .../samples/9.x}/WebMinOpenApi/nuget.config | 0 .../9.x}/WebMinOpenApi/projectFile.xml | 0 .../includes/transformer-registration.md | 8 +- aspnetcore/toc.yml | 2 + 14 files changed, 164 insertions(+), 21 deletions(-) create mode 100644 aspnetcore/fundamentals/openapi/buildtime-openapi.md rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/.spectral.yaml (100%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/Program.cs (93%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/WebMinOpenApi.csproj (75%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/WebMinOpenApi.json (87%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/appsettings.Development.json (100%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/appsettings.json (100%) create mode 100644 aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/global.json create mode 100644 aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/my-open-api.json rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/nuget.config (100%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/projectFile.xml (100%) diff --git a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md index 0f2d45c32400..0f6d921ddb9b 100644 --- a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md +++ b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md @@ -427,7 +427,7 @@ Transformers fall into two categories: * Document transformers have access to the entire OpenAPI document. These can be used to make global modifications to the document. * Operation transformers apply to each individual operation. Each individual operation is a combination of path and HTTP method. These can be used to modify parameters or responses on endpoints. -Transformers can be registered onto the document via the `UseTransformer` call on the `OpenApiOptions` object. The following snippet shows different ways to register transformers onto the document: +Transformers can be registered onto the document via the `AddDocumentTransformer` call on the `OpenApiOptions` object. The following snippet shows different ways to register transformers onto the document: * Register a document transformer using a delegate. * Register a document transformer using an instance of `IOpenApiDocumentTransformer`. diff --git a/aspnetcore/fundamentals/openapi/buildtime-openapi.md b/aspnetcore/fundamentals/openapi/buildtime-openapi.md new file mode 100644 index 000000000000..5fbf081b8483 --- /dev/null +++ b/aspnetcore/fundamentals/openapi/buildtime-openapi.md @@ -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 + + ./ + +``` + +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 + + --file-name my-open-api + +``` + +### 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 + + --document-name v2 + +``` + +## 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(); +} +``` diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/.spectral.yaml b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/.spectral.yaml similarity index 100% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/.spectral.yaml rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/.spectral.yaml diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs similarity index 93% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs index 6697c5e210a3..5dcca7ab1379 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs +++ b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs @@ -55,7 +55,7 @@ internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary builder.Services.AddOpenApi(options => { - options.UseTransformer((document, context, cancellationToken) => + options.AddDocumentTransformer((document, context, cancellationToken) => { document.Info = new() { @@ -91,7 +91,7 @@ internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary builder.Services.AddOpenApi(options => { - options.UseTransformer(); + options.AddDocumentTransformer(); }); var app = builder.Build(); @@ -141,7 +141,7 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf builder.Services.AddOpenApi(options => { - options.UseOperationTransformer((operation, context, cancellationToken) => + options.AddOperationTransformer((operation, context, cancellationToken) => { operation.Responses.Add("500", new OpenApiResponse { Description = "Internal server error" }); return Task.CompletedTask; @@ -172,7 +172,7 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf builder.Services.AddOpenApi("internal", options => { - options.UseTransformer(); + options.AddDocumentTransformer(); }); builder.Services.AddOpenApi("public"); @@ -360,11 +360,11 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf builder.Services.AddOpenApi(options => { - options.UseTransformer((document, context, cancellationToken) + options.AddDocumentTransformer((document, context, cancellationToken) => Task.CompletedTask); - options.UseTransformer(new MyDocumentTransformer()); - options.UseTransformer(); - options.UseOperationTransformer((operation, context, cancellationToken) + options.AddDocumentTransformer(new MyDocumentTransformer()); + options.AddDocumentTransformer(); + options.AddOperationTransformer((operation, context, cancellationToken) => Task.CompletedTask); }); @@ -396,9 +396,9 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC builder.Services.AddOpenApi(options => { - options.UseOperationTransformer((operation, context, cancellationToken) + options.AddOperationTransformer((operation, context, cancellationToken) => Task.CompletedTask); - options.UseTransformer((document, context, cancellationToken) + options.AddDocumentTransformer((document, context, cancellationToken) => Task.CompletedTask); }); diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.csproj similarity index 75% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.csproj index 27fa91627cae..a4d4c83ddce2 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj +++ b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.csproj @@ -4,6 +4,8 @@ net9.0 enable enable + ./ + --file-name my-open-api @@ -12,14 +14,14 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.json similarity index 87% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.json index 7aa05da9c5fb..efd284120963 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json +++ b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.json @@ -21,11 +21,11 @@ } } } - }, - "x-aspnetcore-id": "14ccb7f6-1846-48e4-aeaf-c760aadda7c3" + } } } }, + "components": { }, "tags": [ { "name": "GetDocument.Insider" diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/appsettings.Development.json b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/appsettings.Development.json similarity index 100% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/appsettings.Development.json rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/appsettings.Development.json diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/appsettings.json b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/appsettings.json similarity index 100% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/appsettings.json rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/appsettings.json diff --git a/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/global.json b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/global.json new file mode 100644 index 000000000000..6091d76c863a --- /dev/null +++ b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "9.0.100-rc.1.24414.13" + } +} diff --git a/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/my-open-api.json b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/my-open-api.json new file mode 100644 index 000000000000..efd284120963 --- /dev/null +++ b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/my-open-api.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/nuget.config b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/nuget.config similarity index 100% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/nuget.config rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/nuget.config diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/projectFile.xml b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/projectFile.xml similarity index 100% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/projectFile.xml rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/projectFile.xml diff --git a/aspnetcore/release-notes/aspnetcore-9/includes/transformer-registration.md b/aspnetcore/release-notes/aspnetcore-9/includes/transformer-registration.md index 26fd81b8d0df..5b06ecc63fbb 100644 --- a/aspnetcore/release-notes/aspnetcore-9/includes/transformer-registration.md +++ b/aspnetcore/release-notes/aspnetcore-9/includes/transformer-registration.md @@ -5,11 +5,11 @@ OpenAPI transformers support modifying the OpenAPI document, operations within t Previously, the following APIs were available for registering transformers: ```csharp -OpenApiOptions UseTransformer(Func transformer) -OpenApiOptions UseTransformer(IOpenApiDocumentTransformer transformer) -OpenApiOptions UseTransformer() +OpenApiOptions AddDocumentTransformer(Func transformer) +OpenApiOptions AddDocumentTransformer(IOpenApiDocumentTransformer transformer) +OpenApiOptions AddDocumentTransformer() OpenApiOptions UseSchemaTransformer(Func) -OpenApiOptions UseOperationTransformer(Func) +OpenApiOptions AddOperationTransformer(Func) ``` The new API set is as follows: diff --git a/aspnetcore/toc.yml b/aspnetcore/toc.yml index 357c1fcd19e5..136a512d1164 100644 --- a/aspnetcore/toc.yml +++ b/aspnetcore/toc.yml @@ -846,6 +846,8 @@ items: uid: fundamentals/openapi/overview - name: Work with OpenAPI documents uid: fundamentals/openapi/aspnetcore-openapi + - name: Generate OpenAPI documents at build-time + uid: fundamentals/openapi/buildtime-openapi - name: OpenAPI tools uid: fundamentals/openapi/openapi-tools - name: Swagger / NSWag