|
| 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: 12/17/2024 |
| 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 `WithOpenApi` extension methods in `Microsoft.AspNetCore.OpenApi.OpenApiEndpointConventionBuilderExtensions` have been deprecated in .NET 10. Invoking these methods now produces the compile-time diagnostic **ASPDEPR002** and a standard `[Obsolete]` warning. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET 10 Preview 7 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +Previously, you could use the `WithOpenApi` extension method without any warnings: |
| 20 | + |
| 21 | +```csharp |
| 22 | +app.MapGet("/weather", () => ...) |
| 23 | + .WithOpenApi(); // no warnings |
| 24 | +``` |
| 25 | + |
| 26 | +## New behavior |
| 27 | + |
| 28 | +Starting in .NET 10 Preview 7, using the `WithOpenApi` extension method produces a compiler warning: |
| 29 | + |
| 30 | +```csharp |
| 31 | +app.MapGet("/weather", () => ...) |
| 32 | + .WithOpenApi(); // warning ASPDEPR002: WithOpenApi is deprecated and will be removed in a future release. For more information, visit https://aka.ms/aspnet/deprecate/002. |
| 33 | +``` |
| 34 | + |
| 35 | +The call still compiles and executes, but the build now emits the new deprecation warning. |
| 36 | + |
| 37 | +## Type of breaking change |
| 38 | + |
| 39 | +This is a [behavioral change](../../categories.md#behavioral-change). |
| 40 | + |
| 41 | +## Reason for change |
| 42 | + |
| 43 | +`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. |
| 44 | + |
| 45 | +## Recommended action |
| 46 | + |
| 47 | +Remove `.WithOpenApi()` calls from your code. |
| 48 | + |
| 49 | +- If using `Microsoft.AspNetCore.OpenApi` for document generation, use the `AddOpenApiOperationTransformer` extension method. |
| 50 | + |
| 51 | +**Before** |
| 52 | + |
| 53 | +```csharp |
| 54 | +using Microsoft.AspNetCore.OpenApi; |
| 55 | + |
| 56 | +var builder = WebApplication.CreateBuilder(); |
| 57 | +var app = builder.Build(); |
| 58 | + |
| 59 | +app.MapGet("/weather", () => ...) |
| 60 | + .WithOpenApi(operation => |
| 61 | + { |
| 62 | + // Per-endpoint tweaks |
| 63 | + operation.Summary = "Gets the current weather report."; |
| 64 | + operation.Description = "Returns a short description and emoji."; |
| 65 | + return operation; |
| 66 | + }); |
| 67 | + |
| 68 | +app.Run(); |
| 69 | +``` |
| 70 | + |
| 71 | +**After** |
| 72 | + |
| 73 | +```csharp |
| 74 | +using Microsoft.AspNetCore.OpenApi; |
| 75 | + |
| 76 | +var builder = WebApplication.CreateBuilder(); |
| 77 | +var app = builder.Build(); |
| 78 | + |
| 79 | +app.MapGet("/weather", () => ...) |
| 80 | + .AddOpenApiOperationTransformer((operation, context, ct) => |
| 81 | + { |
| 82 | + // Per-endpoint tweaks |
| 83 | + operation.Summary = "Gets the current weather report."; |
| 84 | + operation.Description = "Returns a short description and emoji."; |
| 85 | + return Task.CompletedTask; |
| 86 | + }); |
| 87 | + |
| 88 | +app.Run(); |
| 89 | +``` |
| 90 | + |
| 91 | +- If using `Swashbuckle` for document generation, use the `IOperationFilter` API. |
| 92 | +- If using `NSwag` for document generation, use the `IOperationProcessor` API. |
| 93 | + |
| 94 | +## Affected APIs |
| 95 | + |
| 96 | +- <xref:Microsoft.AspNetCore.OpenApi.OpenApiEndpointConventionBuilderExtensions.WithOpenApi%2A?displayProperty=fullName> |
0 commit comments