|
| 1 | +--- |
| 2 | +title: ASPDEPR002 warning |
| 3 | +description: Learn about the obsoletions that generate compile-time warning ASPDEPR002. |
| 4 | +ai-usage: ai-assisted |
| 5 | +monikerRange: '>= aspnetcore-10.0' |
| 6 | +ms.date: 03/03/2026 |
| 7 | +uid: diagnostics/aspdepr002 |
| 8 | +ms.author: tdykstra |
| 9 | +author: tdykstra |
| 10 | +f1_keywords: |
| 11 | + - aspdepr002 |
| 12 | +--- |
| 13 | +# ASPDEPR002: `WithOpenApi` is deprecated |
| 14 | + |
| 15 | +The <xref:Microsoft.AspNetCore.Builder.OpenApiEndpointConventionBuilderExtensions.WithOpenApi*> extension methods are deprecated starting in .NET 10. Using these methods produces the `ASPDEPR002` diagnostic at compile time. The functionality they provided is now available through the built-in OpenAPI document generation pipeline. |
| 16 | + |
| 17 | +## Workarounds |
| 18 | + |
| 19 | +Remove `.WithOpenApi()` calls from your code. |
| 20 | + |
| 21 | +- If you used `Microsoft.AspNetCore.OpenApi` for document generation, use the `AddOpenApiOperationTransformer` extension method instead. |
| 22 | + |
| 23 | + Before: |
| 24 | + |
| 25 | + ```csharp |
| 26 | + using Microsoft.AspNetCore.OpenApi; |
| 27 | + |
| 28 | + var builder = WebApplication.CreateBuilder(); |
| 29 | + var app = builder.Build(); |
| 30 | + |
| 31 | + app.MapGet("/weather", () => ...) |
| 32 | + .WithOpenApi(operation => |
| 33 | + { |
| 34 | + // Per-endpoint tweaks |
| 35 | + operation.Summary = "Gets the current weather report."; |
| 36 | + operation.Description = "Returns a short description and emoji."; |
| 37 | + return operation; |
| 38 | + }); |
| 39 | + |
| 40 | + app.Run(); |
| 41 | + ``` |
| 42 | + |
| 43 | + After: |
| 44 | + |
| 45 | + ```csharp |
| 46 | + using Microsoft.AspNetCore.OpenApi; |
| 47 | + |
| 48 | + var builder = WebApplication.CreateBuilder(); |
| 49 | + var app = builder.Build(); |
| 50 | + |
| 51 | + app.MapGet("/weather", () => ...) |
| 52 | + .AddOpenApiOperationTransformer((operation, context, ct) => |
| 53 | + { |
| 54 | + // Per-endpoint tweaks |
| 55 | + operation.Summary = "Gets the current weather report."; |
| 56 | + operation.Description = "Returns a short description and emoji."; |
| 57 | + return Task.CompletedTask; |
| 58 | + }); |
| 59 | + |
| 60 | + app.Run(); |
| 61 | + ``` |
| 62 | + |
| 63 | +- If you used `Swashbuckle` for document generation, use the `IOperationFilter` API. |
| 64 | +- If you used `NSwag` for document generation, use the `IOperationProcessor` API. |
| 65 | + |
| 66 | +## Suppress a warning |
| 67 | + |
| 68 | +If you must use the deprecated APIs, you can suppress the warning in code or in your project file. |
| 69 | + |
| 70 | +To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning. |
| 71 | + |
| 72 | +```csharp |
| 73 | +// Disable the warning. |
| 74 | +#pragma warning disable ASPDEPR002 |
| 75 | + |
| 76 | +// Code that uses deprecated API. |
| 77 | +// ... |
| 78 | +
|
| 79 | +// Re-enable the warning. |
| 80 | +#pragma warning restore ASPDEPR002 |
| 81 | +``` |
| 82 | + |
| 83 | +To suppress all the `ASPDEPR002` warnings in your project, add a `<NoWarn>` property to your project file. |
| 84 | + |
| 85 | +```xml |
| 86 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 87 | + <PropertyGroup> |
| 88 | + ... |
| 89 | + <NoWarn>$(NoWarn);ASPDEPR002</NoWarn> |
| 90 | + </PropertyGroup> |
| 91 | +</Project> |
| 92 | +``` |
| 93 | + |
| 94 | +## See also |
| 95 | + |
| 96 | +- [Deprecation of WithOpenApi extension method](../breaking-changes/10/withopenapi-deprecated.md) |
0 commit comments