diff --git a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md index 06cb11ba6f90..1462553f00ac 100644 --- a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md +++ b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md @@ -67,7 +67,7 @@ The following code: * Adds OpenAPI services. * Enables the endpoint for viewing the OpenAPI document in JSON format. -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,7)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,7)] Launch the app and navigate to `https://localhost:/openapi/v1.json` to view the generated OpenAPI document. @@ -649,13 +649,13 @@ Because the OpenAPI document is served via a route handler endpoint, any customi The OpenAPI endpoint doesn't enable any authorization checks by default. However, authorization checks can be applied to the OpenAPI document. In the following code, access to the OpenAPI document is limited to those with the `tester` role: -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_mapopenapiwithauth)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_mapopenapiwithauth)] #### Cache generated OpenAPI document 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. -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_mapopenapiwithcaching)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_mapopenapiwithcaching)] @@ -689,13 +689,13 @@ Transformers can be registered onto the document by calling the . * Register a schema transformer using a DI-activated . -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_transUse&highlight=8-19)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_transUse&highlight=8-19)] ### Execution order for transformers Transformers execute in first-in first-out order based on registration. In the following snippet, the document transformer has access to the modifications made by the operation transformer: -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_transInOut&highlight=3-9)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_transInOut&highlight=3-9)] ### Use document transformers @@ -707,18 +707,18 @@ Document transformers have access to a context object that includes: Document transformers can also mutate the OpenAPI document that is generated. The following example demonstrates a document transformer that adds some information about the API to the OpenAPI document. -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_documenttransformer1)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_documenttransformer1)] Service-activated document transformers can utilize instances from DI to modify the app. The following sample demonstrates a document transformer that uses the service from the authentication layer. It checks if any JWT bearer-related schemes are registered in the app and adds them to the OpenAPI document's top level: -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_documenttransformer2)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_documenttransformer2)] Document transformers are unique to the document instance they're associated with. In the following example, a transformer: * Registers authentication-related requirements to the `internal` document. * Leaves the `public` document unmodified. -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_multidoc_operationtransformer1)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_multidoc_operationtransformer1)] ### Use operation transformers @@ -735,7 +735,7 @@ Operation transformers have access to a context object which contains: For example, the following operation transformer adds `500` as a response status code supported by all operations in the document. -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_operationtransformer1)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_operationtransformer1)] ### Use schema transformers @@ -752,7 +752,7 @@ Schema transformers have access to a context object which contains: For example, the following schema transformer sets the `format` of decimal types to `decimal` instead of `double`: -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_schematransformer1)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_schematransformer1)] ## Additional resources diff --git a/aspnetcore/fundamentals/openapi/buildtime-openapi.md b/aspnetcore/fundamentals/openapi/buildtime-openapi.md new file mode 100644 index 000000000000..caabedf805d9 --- /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 typical web applications, OpenAPI documents are generated at run-time and served via an HTTP request to the application server. + +In some scenarios, it's helpful to generate the OpenAPI document during the application's build step. These scenarios include: + +- 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/Debug/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") +{ + builder.Services.AddDefaults(); +} +``` diff --git a/aspnetcore/fundamentals/openapi/overview.md b/aspnetcore/fundamentals/openapi/overview.md index afa7c27b5291..10d34611226b 100644 --- a/aspnetcore/fundamentals/openapi/overview.md +++ b/aspnetcore/fundamentals/openapi/overview.md @@ -27,7 +27,7 @@ ASP.NET Core apps provide built-in support for generating information about endp The following code is generated by the ASP.NET Core minimal web API template and uses OpenAPI: -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_default&highlight=5,9-12)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_default&highlight=5,9-12)] In the preceding highlighted code: @@ -45,7 +45,7 @@ The [`Microsoft.AspNetCore.OpenApi`](https://www.nuget.org/packages/Microsoft.As To use the `Microsoft.AspNetCore.OpenApi` package, add it as a PackageReference to a project file: -[!code-xml[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/projectFile.xml?highlight=15)] +[!code-xml[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/projectFile.xml?highlight=15)] To learn more about the `Microsoft.AspNetCore.OpenApi` package, see . @@ -58,7 +58,7 @@ Document generation at build time is enabled by setting the `OpenApiGenerateDocu By default, the generated OpenAPI document is saved to the `obj` directory, but you can customize the output directory by setting the `OpenApiDocumentsDirectory` property. -[!code-xml[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/projectFile.xml?highlight=9-12,16-19)] +[!code-xml[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/projectFile.xml?highlight=9-12,16-19)] ## ASP.NET Core OpenAPI source code on GitHub 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 100% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs 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 85% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.csproj index 2a67e5a014e3..a23caf39a7e9 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 @@ -19,7 +21,7 @@ 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 100% rename from aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json rename to aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/WebMinOpenApi.json 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/fundamentals/openapi/using-openapi-documents.md b/aspnetcore/fundamentals/openapi/using-openapi-documents.md index 6361cc6d6ade..da6b30c509cf 100644 --- a/aspnetcore/fundamentals/openapi/using-openapi-documents.md +++ b/aspnetcore/fundamentals/openapi/using-openapi-documents.md @@ -22,13 +22,13 @@ The `Swashbuckle.AspNetCore.SwaggerUi` package provides a bundle of Swagger UI's * Enable the swagger-ui middleware with a reference to the [OpenAPI route registered earlier](xref:fundamentals/openapi/aspnetcore-openapi#customize-the-openapi-endpoint-route). * To limit information disclosure and security vulnerability, ***only enable Swagger UI in development environments.*** -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_swaggerui)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_swaggerui)] ## Use Scalar for interactive API documentation [Scalar](https://scalar.com/) is an open-source interactive document UI for OpenAPI. Scalar can integrate with the OpenAPI endpoint provided by ASP.NET Core. To configure Scalar, install the `Scalar.AspNetCore` package. -[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_openapiwithscalar)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_openapiwithscalar)] ## Lint generated OpenAPI documents with Spectral @@ -76,4 +76,4 @@ The output shows any issues with the OpenAPI document. For example: ``` :::moniker-end -[!INCLUDE[](~/fundamentals/openapi/includes/using-openapi-documents-6-8.md)] \ No newline at end of file +[!INCLUDE[](~/fundamentals/openapi/includes/using-openapi-documents-6-8.md)] 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 85ed4eb7d7f7..1a91d289a50e 100644 --- a/aspnetcore/toc.yml +++ b/aspnetcore/toc.yml @@ -878,6 +878,8 @@ items: uid: fundamentals/openapi/aspnetcore-openapi - name: Use OpenAPI documents uid: fundamentals/openapi/using-openapi-documents + - name: Generate OpenAPI documents at build-time + uid: fundamentals/openapi/buildtime-openapi - name: OpenAPI tools uid: fundamentals/openapi/openapi-tools - name: Swagger / NSwag