Skip to content

Commit b6892c7

Browse files
authored
Add injecting IOpenApiDocumentProvider support to using-openapi-documents (#35888)
* Add injecting IOpenApiDocumentProvider suport to using-openapi-documents.md * Added code sample for OpenApiDocumentProvider * Give github link relative path * Moved github link back to absolute URL * Reduced example size to focus on DI * Removed changes on program.cs
1 parent e934eb3 commit b6892c7

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

aspnetcore/fundamentals/openapi/using-openapi-documents.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to use OpenAPI documents in an ASP.NET Core app.
55
ms.author: safia
66
monikerRange: '>= aspnetcore-6.0'
77
ms.custom: mvc
8-
ms.date: 05/09/2025
8+
ms.date: 08/04/2025
99
uid: fundamentals/openapi/using-openapi-documents
1010
---
1111
# Use openAPI documents
@@ -14,7 +14,7 @@ uid: fundamentals/openapi/using-openapi-documents
1414

1515
## Use Swagger UI for local ad-hoc testing
1616

17-
By default, the `Microsoft.AspNetCore.OpenApi` package doesn't ship with built-in support for visualizing or interacting with the OpenAPI document. Popular tools for visualizing or interacting with the OpenAPI document include [Swagger UI](https://swagger.io/tools/swaggerhub/) and [ReDoc](https://github.com/Redocly/redoc). Swagger UI and ReDoc can be integrated in an app in several ways. Editors such as Visual Studio and VS Code offer extensions and built-in experiences for testing against an OpenAPI document.
17+
By default, the `Microsoft.AspNetCore.OpenApi` package doesn't ship with built-in support for visualizing or interacting with the OpenAPI document. Popular tools for visualizing or interacting with the OpenAPI document include [Swagger UI](https://swagger.io/tools/swaggerhub/) and [ReDoc](https://github.com/Redocly/redoc). Swagger UI and ReDoc can be integrated in an app in several ways. Editors such as Visual Studio and Visual Studio Code offer extensions and built-in experiences for testing against an OpenAPI document.
1818

1919
The `Swashbuckle.AspNetCore.SwaggerUi` package provides a bundle of Swagger UI's web assets for use in apps. This package can be used to render a UI for the generated document. To configure this:
2020

@@ -59,7 +59,7 @@ To automatically launch the app at the Scalar UI URL using the `https` profile o
5959

6060
[Spectral](https://stoplight.io/open-source/spectral) is an open-source OpenAPI document linter. Spectral can be incorporated into an app build to verify the quality of generated OpenAPI documents. Install Spectral according to the [package installation directions](https://github.com/stoplightio/spectral#-installation).
6161

62-
To take advantage of Spectral, install the `Microsoft.Extensions.ApiDescription.Server` package to enable build-time OpenAPI document generation.
62+
To take advantage of Spectral for linting OpenAPI documents at build time, first install the `Microsoft.Extensions.ApiDescription.Server` package to enable build-time OpenAPI document generation.
6363

6464
Enable document generation at build time by setting the following properties in the app's `.csproj` file":
6565

@@ -99,6 +99,37 @@ The output shows any issues with the OpenAPI document. For example:
9999
100100
✖ 5 problems (0 errors, 5 warnings, 0 infos, 0 hints)
101101
```
102+
103+
## Support for injecting `IOpenApiDocumentProvider`
104+
105+
You can inject <xref:Microsoft.AspNetCore.OpenApi.Services.IOpenApiDocumentProvider> into your services through dependency injection to access OpenAPI documents programmatically, even outside HTTP request contexts.
106+
107+
```csharp
108+
public class DocumentService
109+
{
110+
private readonly IOpenApiDocumentProvider _documentProvider;
111+
112+
public DocumentService(IOpenApiDocumentProvider documentProvider)
113+
{
114+
_documentProvider = documentProvider;
115+
}
116+
117+
public async Task<OpenApiDocument> GetApiDocumentAsync()
118+
{
119+
return await _documentProvider.GetOpenApiDocumentAsync("v1");
120+
}
121+
}
122+
```
123+
124+
Register the service in your DI container:
125+
126+
```csharp
127+
builder.Services.AddScoped<DocumentService>();
128+
```
129+
130+
This enables scenarios such as generating client SDKs, validating API contracts in background processes, or exporting documents to external systems.
131+
132+
Support for injecting `IOpenApiDocumentProvider` was introduced in ASP.NET Core in .NET 10. For more information, see [dotnet/aspnetcore #61463](https://github.com/dotnet/aspnetcore/pull/61463).
102133
:::moniker-end
103134

104135
[!INCLUDE[](~/fundamentals/openapi/includes/using-openapi-documents-6-8.md)]

0 commit comments

Comments
 (0)