|
| 1 | +--- |
| 2 | +title: "Breaking change: Deprecation of WithOpenApi extension method" |
| 3 | +description: "Learn about the breaking change in ASP.NET Core 10 where WithOpenApi extension methods have been deprecated and produce a compiler warning." |
| 4 | +ms.date: 08/07/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/aspnet/Announcements/issues/519 |
| 7 | +--- |
| 8 | + |
| 9 | +# Deprecation of WithOpenApi extension method |
| 10 | + |
| 11 | +The <xref:Microsoft.AspNetCore.Builder.OpenApiEndpointConventionBuilderExtensions.WithOpenApi*> methods have been deprecated in .NET 10. Invoking these methods now produces the compile-time diagnostic `ASPDEPR002` and a standard `Obsolete` warning that states: |
| 12 | + |
| 13 | +> WithOpenApi is deprecated and will be removed in a future release. For more information, visit <https://aka.ms/aspnet/deprecate/002>. |
| 14 | +
|
| 15 | +## Version introduced |
| 16 | + |
| 17 | +.NET 10 Preview 7 |
| 18 | + |
| 19 | +## Previous behavior |
| 20 | + |
| 21 | +Previously, you could use the `WithOpenApi` extension method without any warnings: |
| 22 | + |
| 23 | +```csharp |
| 24 | +app.MapGet("/weather", () => ...) |
| 25 | + .WithOpenApi(); // No warnings. |
| 26 | +``` |
| 27 | + |
| 28 | +## New behavior |
| 29 | + |
| 30 | +Starting in .NET 10, using the `WithOpenApi` extension method produces a compiler warning: |
| 31 | + |
| 32 | +```csharp |
| 33 | +app.MapGet("/weather", () => ...) |
| 34 | + .WithOpenApi(); // Warning ASPDEPR002: WithOpenApi is deprecated... |
| 35 | +``` |
| 36 | + |
| 37 | +However, the call still compiles and executes. |
| 38 | + |
| 39 | +## Type of breaking change |
| 40 | + |
| 41 | +This change can affect [source compatibility](../../categories.md#source-compatibility). |
| 42 | + |
| 43 | +## Reason for change |
| 44 | + |
| 45 | +<xref:Microsoft.AspNetCore.Builder.OpenApiEndpointConventionBuilderExtensions.WithOpenApi*> duplicated functionality now provided by the built-in OpenAPI document generation pipeline. Deprecating it simplifies the API surface and prepares for its eventual removal. |
| 46 | + |
| 47 | +## Recommended action |
| 48 | + |
| 49 | +Remove `.WithOpenApi()` calls from your code. |
| 50 | + |
| 51 | +- If you used `Microsoft.AspNetCore.OpenApi` for document generation, use the <xref:Microsoft.AspNetCore.Builder.OpenApiEndpointConventionBuilderExtensions.AddOpenApiOperationTransformer``1(``0,System.Func{Microsoft.OpenApi.OpenApiOperation,Microsoft.AspNetCore.OpenApi.OpenApiOperationTransformerContext,System.Threading.CancellationToken,System.Threading.Tasks.Task})> extension method. |
| 52 | + |
| 53 | + Before: |
| 54 | + |
| 55 | + ```csharp |
| 56 | + using Microsoft.AspNetCore.OpenApi; |
| 57 | + |
| 58 | + var builder = WebApplication.CreateBuilder(); |
| 59 | + var app = builder.Build(); |
| 60 | + |
| 61 | + app.MapGet("/weather", () => ...) |
| 62 | + .WithOpenApi(operation => |
| 63 | + { |
| 64 | + // Per-endpoint tweaks |
| 65 | + operation.Summary = "Gets the current weather report."; |
| 66 | + operation.Description = "Returns a short description and emoji."; |
| 67 | + return operation; |
| 68 | + }); |
| 69 | + |
| 70 | + app.Run(); |
| 71 | + ``` |
| 72 | + |
| 73 | + After: |
| 74 | + |
| 75 | + ```csharp |
| 76 | + using Microsoft.AspNetCore.OpenApi; |
| 77 | + |
| 78 | + var builder = WebApplication.CreateBuilder(); |
| 79 | + var app = builder.Build(); |
| 80 | + |
| 81 | + app.MapGet("/weather", () => ...) |
| 82 | + .AddOpenApiOperationTransformer((operation, context, ct) => |
| 83 | + { |
| 84 | + // Per-endpoint tweaks |
| 85 | + operation.Summary = "Gets the current weather report."; |
| 86 | + operation.Description = "Returns a short description and emoji."; |
| 87 | + return Task.CompletedTask; |
| 88 | + }); |
| 89 | + |
| 90 | + app.Run(); |
| 91 | + ``` |
| 92 | + |
| 93 | +- If you used `Swashbuckle` for document generation, use the `IOperationFilter` API. |
| 94 | +- If you used `NSwag` for document generation, use the `IOperationProcessor` API. |
| 95 | + |
| 96 | +## Affected APIs |
| 97 | + |
| 98 | +- <xref:Microsoft.AspNetCore.Builder.OpenApiEndpointConventionBuilderExtensions.WithOpenApi%2A?displayProperty=fullName> |
0 commit comments