Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions aspnetcore/fundamentals/openapi/aspnetcore-openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Learn how to generate and customize OpenAPI documents in an ASP.NET
ms.author: safia
monikerRange: '>= aspnetcore-6.0'
ms.custom: mvc
ms.date: 09/05/2024
ms.date: 12/11/2024
uid: fundamentals/openapi/aspnetcore-openapi
---
# Generate OpenAPI documents
Expand Down Expand Up @@ -188,26 +188,15 @@ Some applications may be configured to emit multiple OpenAPI documents, for vari

### 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:
Build-time OpenAPI document generation functions by launching the apps entrypoint with a mock server implementation. A mock server is required to produce accurate OpenAPI documents because all information in the OpenAPI document can't be statically analyzed. Because the apps entrypoint is invoked, any logic in the apps startup is invoked. This includes code that injects services into the [DI container](xref:fundamentals/dependency-injection) or reads from configuration. In some scenarios, it's necessary to restrict the code paths that will run when the apps 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:
In order to restrict these code paths from being invoked by the build-time generation pipeline, they can be conditioned behind a check of the entry assembly:

```csharp
using System.Reflection;

var builder = WebApplication.CreateBuilder();
:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/GetDocument.Insider/Program.cs" highlight="4-8":::

if (Assembly.GetEntryAssembly()?.GetName().Name != "GetDocument.Insider")
{
builder.Services.AddDefaults();
}
```

::: moniker-end
:::moniker-end

[!INCLUDE[](~/fundamentals/openapi/includes/aspnetcore-openapi6-8.md)]


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenApi();

if (Assembly.GetEntryAssembly()?.GetName().Name != "GetDocument.Insider")
{
// 'IServiceCollection' does not contain a definition for 'AddDefaults'
builder.AddServiceDefaults();
}
var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

var myKeyValue = app.Configuration["MyKey"];

app.MapGet("/", () => {
return Results.Ok($"The value of MyKey is: {myKeyValue}");
})
.WithName("TestKey");

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"MyKey": "My Key from appsettings.json"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-rc.1.24452.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0-rc.1.24452.1" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.0-rc.1.24452.1">
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Scalar.AspNetCore" Version="1.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="6.7.0" />
<PackageReference Include="Scalar.AspNetCore" Version="1.2.50" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="7.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"openapi": "3.0.1",
"info": {
"title": "GetDocument.Insider | v1",
"description": "Transformed OpenAPI document",
"version": "1.0.0"
},
"paths": {
Expand All @@ -10,13 +11,15 @@
"tags": [
"GetDocument.Insider"
],
"summary": "Transformed OpenAPI operation",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
"type": "string",
"description": "Transformed OpenAPI schema"
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions aspnetcore/includes/add-EF-NuGet-SQLite-CLI-9.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ dotnet tool uninstall --global dotnet-aspnet-codegenerator
dotnet tool install --global dotnet-aspnet-codegenerator
dotnet tool uninstall --global dotnet-ef
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design --prerelease
dotnet add package Microsoft.EntityFrameworkCore.SQLite --prerelease
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --prerelease
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --prerelease
dotnet add package Microsoft.EntityFrameworkCore.Tools --prerelease
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SQLite
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
```

The preceding commands add:
Expand Down
4 changes: 2 additions & 2 deletions aspnetcore/performance/caching/hybrid.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ In such cases, inform `HybridCache` that it's safe to reuse instances by:
Here are the .NET CLI commands to install the packages:

```dotnetcli
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis --prerelease
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
```

```dotnetcli
dotnet add package Microsoft.Extensions.Caching.SqlServer --prerelease
dotnet add package Microsoft.Extensions.Caching.SqlServer
```

## Custom HybridCache implementations
Expand Down
4 changes: 2 additions & 2 deletions aspnetcore/release-notes/aspnetcore-9/includes/openApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The following highlighted code calls:
Install the [`Microsoft.AspNetCore.OpenApi`](https://www.nuget.org/packages/Microsoft.AspNetCore.OpenApi) package in the project using the following command:

```dotnetcli
dotnet add package Microsoft.AspNetCore.OpenApi --prerelease
dotnet add package Microsoft.AspNetCore.OpenApi
```

Run the app and navigate to `openapi/v1.json` to view the generated OpenAPI document:
Expand All @@ -22,7 +22,7 @@ Run the app and navigate to `openapi/v1.json` to view the generated OpenAPI docu
OpenAPI documents can also be generated at build-time by adding the [`Microsoft.Extensions.ApiDescription.Server`](https://www.nuget.org/packages/Microsoft.Extensions.ApiDescription.Server) package:

```dotnetcli
dotnet add package Microsoft.Extensions.ApiDescription.Server --prerelease
dotnet add package Microsoft.Extensions.ApiDescription.Server
```

To modify the location of the emitted OpenAPI documents, set the target path in the OpenApiDocumentsDirectory property in the app's project file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dotnet new webapiaot
Add the Microsoft.AspNetCore.OpenAPI package.

```console
dotnet add package Microsoft.AspNetCore.OpenApi --prerelease
dotnet add package Microsoft.AspNetCore.OpenApi
```

For this preview, you also need to add the latest Microsoft.OpenAPI package to avoid trimming warnings.
Expand Down
4 changes: 2 additions & 2 deletions aspnetcore/tutorials/min-web-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ NuGet packages must be added to support the database and diagnostics used in thi
* Run the following commands:

```dotnetcli
dotnet add package Microsoft.EntityFrameworkCore.InMemory --prerelease
dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore --prerelease
dotnet add package Microsoft.EntityFrameworkCore.InMemory
dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
```

---
Expand Down
Loading