From 2f05a5ab2928d851bad33a3ea9e2948f131532e5 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Wed, 14 Aug 2024 13:06:29 -0700 Subject: [PATCH 1/4] Add build-time OpenAPI generation docs --- .../fundamentals/openapi/buildtime-openapi.md | 100 ++++++++++++++++++ .../samples/9.x}/WebMinOpenApi/.spectral.yaml | 0 .../samples/9.x}/WebMinOpenApi/Program.cs | 0 .../9.x}/WebMinOpenApi/WebMinOpenApi.csproj | 4 +- .../9.x}/WebMinOpenApi/WebMinOpenApi.json | 0 .../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 + 13 files changed, 148 insertions(+), 5 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 (100%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/WebMinOpenApi.csproj (85%) rename aspnetcore/fundamentals/{minimal-apis/9.0-samples => openapi/samples/9.x}/WebMinOpenApi/WebMinOpenApi.json (100%) 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/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 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/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 From 0f130fcce4b385c7a0227b24f47cfa7824995449 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Thu, 24 Oct 2024 17:33:17 -0700 Subject: [PATCH 2/4] Fix references to sample app --- .../fundamentals/minimal-apis/responses.md | 22 +++++++++---------- .../openapi/aspnetcore-openapi.md | 20 ++++++++--------- aspnetcore/fundamentals/openapi/overview.md | 6 ++--- .../openapi/using-openapi-documents.md | 4 ++-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index 0a7714c7ae59..771169701974 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -28,7 +28,7 @@ Minimal endpoints support the following types of return values: Consider the following route handler, which returns a `Hello world` text. -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_01"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_01"::: The `200` status code is returned with `text/plain` Content-Type header and the following content. @@ -44,7 +44,7 @@ Hello World Consider the following route handler, which returns an anonymous type containing a `Message` string property. -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_02"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_02"::: The `200` status code is returned with `application/json` Content-Type header and the following content. @@ -114,11 +114,11 @@ This has the added benefit of providing compile-time checking that a route handl Consider the following endpoint, for which a `400 BadRequest` status code is returned when the `orderId` is greater than `999`. Otherwise, it produces a `200 OK` with the expected content. -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_03"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_03"::: In order to document this endpoint correctly the extension method `Produces` is called. However, since the `TypedResults` helper automatically includes the metadata for the endpoint, you can return the `Results` union type instead, as shown in the following code. -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_04"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_04"::: @@ -130,7 +130,7 @@ The following sections demonstrate the usage of the common result helpers. #### JSON -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_05"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_05"::: is an alternative way to return JSON: @@ -138,17 +138,17 @@ The following sections demonstrate the usage of the common result helpers. #### Custom Status Code -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_06"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_06"::: #### Internal Server Error -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_07"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_07"::: The preceding example returns a 500 status code. #### Text -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_08"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_08"::: @@ -170,11 +170,11 @@ The following example streams a video from an Azure Blob: #### Redirect -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_09"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_09"::: #### File -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_10"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_10"::: @@ -215,7 +215,7 @@ The `ProducesHtmlMetadata` is an implementation of to describe the produced response. The following code changes the `PopulateMetadata` method to use `ProducesAttribute`. -:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_11"::: +:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_11"::: ## Configure JSON serialization options 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/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/openapi/using-openapi-documents.md b/aspnetcore/fundamentals/openapi/using-openapi-documents.md index 6361cc6d6ade..be6b1206492c 100644 --- a/aspnetcore/fundamentals/openapi/using-openapi-documents.md +++ b/aspnetcore/fundamentals/openapi/using-openapi-documents.md @@ -22,7 +22,7 @@ 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 @@ -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)] From 24bb8b7dce0edab4c42ec8badf27f48fc6a0e764 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 25 Oct 2024 17:25:22 -0700 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Tom Dykstra <1569635+tdykstra@users.noreply.github.com> --- .../fundamentals/openapi/buildtime-openapi.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/aspnetcore/fundamentals/openapi/buildtime-openapi.md b/aspnetcore/fundamentals/openapi/buildtime-openapi.md index 5fbf081b8483..caabedf805d9 100644 --- a/aspnetcore/fundamentals/openapi/buildtime-openapi.md +++ b/aspnetcore/fundamentals/openapi/buildtime-openapi.md @@ -11,13 +11,13 @@ 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 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: +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 +- 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: @@ -42,7 +42,7 @@ Upon installation, this package will automatically generate the Open API documen ```cli $ dotnet build -$ cat bin/Debub/net9.0/{ProjectName}.json +$ cat bin/Debug/net9.0/{ProjectName}.json ``` ## Customizing build-time document generation @@ -83,8 +83,8 @@ Some applications may be configured to emit multiple OpenAPI documents, for vari 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 +- 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: @@ -95,6 +95,6 @@ var builder = WebApplication.CreateBuilder(); if (Assembly.GetEntryAssembly()?.GetName().Name != "GetDocument.Insider") { - builders.Services.AddDefaults(); + builder.Services.AddDefaults(); } ``` From 98d0e57ee099cc42f5f9f9b9d2219e4403117ef2 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Sat, 26 Oct 2024 00:39:32 +0000 Subject: [PATCH 4/4] Fix snippet paths in files --- .../fundamentals/minimal-apis/responses.md | 22 +++++++++---------- .../openapi/using-openapi-documents.md | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index 771169701974..0a7714c7ae59 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -28,7 +28,7 @@ Minimal endpoints support the following types of return values: Consider the following route handler, which returns a `Hello world` text. -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_01"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_01"::: The `200` status code is returned with `text/plain` Content-Type header and the following content. @@ -44,7 +44,7 @@ Hello World Consider the following route handler, which returns an anonymous type containing a `Message` string property. -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_02"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_02"::: The `200` status code is returned with `application/json` Content-Type header and the following content. @@ -114,11 +114,11 @@ This has the added benefit of providing compile-time checking that a route handl Consider the following endpoint, for which a `400 BadRequest` status code is returned when the `orderId` is greater than `999`. Otherwise, it produces a `200 OK` with the expected content. -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_03"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_03"::: In order to document this endpoint correctly the extension method `Produces` is called. However, since the `TypedResults` helper automatically includes the metadata for the endpoint, you can return the `Results` union type instead, as shown in the following code. -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_04"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_04"::: @@ -130,7 +130,7 @@ The following sections demonstrate the usage of the common result helpers. #### JSON -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_05"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_05"::: is an alternative way to return JSON: @@ -138,17 +138,17 @@ The following sections demonstrate the usage of the common result helpers. #### Custom Status Code -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_06"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_06"::: #### Internal Server Error -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_07"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_07"::: The preceding example returns a 500 status code. #### Text -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_08"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_08"::: @@ -170,11 +170,11 @@ The following example streams a video from an Azure Blob: #### Redirect -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_09"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_09"::: #### File -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_10"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_10"::: @@ -215,7 +215,7 @@ The `ProducesHtmlMetadata` is an implementation of to describe the produced response. The following code changes the `PopulateMetadata` method to use `ProducesAttribute`. -:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/Snippets/Program.cs" id="snippet_11"::: +:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_11"::: ## Configure JSON serialization options diff --git a/aspnetcore/fundamentals/openapi/using-openapi-documents.md b/aspnetcore/fundamentals/openapi/using-openapi-documents.md index be6b1206492c..da6b30c509cf 100644 --- a/aspnetcore/fundamentals/openapi/using-openapi-documents.md +++ b/aspnetcore/fundamentals/openapi/using-openapi-documents.md @@ -28,7 +28,7 @@ The `Swashbuckle.AspNetCore.SwaggerUi` package provides a bundle of Swagger UI's [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