Skip to content

Commit c023fca

Browse files
authored
Add documentation about AddOpenApiOperationTransformer (#36207)
1 parent 5824ccc commit c023fca

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

aspnetcore/fundamentals/openapi/customize-openapi.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ For example, the following operation transformer adds `500` as a response status
102102

103103
[!code-csharp[](~/fundamentals/openapi/samples/10.x/WebMinOpenApi/Program.cs?name=snippet_operationtransformer1)]
104104

105+
<!-- UPDATE 10.0 - API doc cross-link:
106+
<xref:Microsoft.AspNetCore.Http.OpenApiRouteHandlerBuilderExtensions.AddOpenApiOperationTransformer%2A>
107+
-->
108+
109+
Operation transformers can also be added to specific endpoint with the `AddOpenApiOperationTransformer` API, instead of all endpoints in a document. This can be useful to change specific OpenAPI data for a specific endpoint, like adding a security scheme, response description or other OpenAPI operation properties. The following example demonstrates adding an operation transformer to a deprecated endpoint specifically, which marks the endpoint as deprecated in the OpenAPI document.
110+
111+
[!code-csharp[](~/fundamentals/openapi/samples/10.x/WebMinOpenApi/Program.cs?name=snippet_operationtransformer2)]
112+
105113
## Use schema transformers
106114

107115
Schemas are the data models that are used in request and response bodies in an OpenAPI document. Schema transformers are useful when a modification:

aspnetcore/fundamentals/openapi/samples/10.x/WebMinOpenApi/Program.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//#define SWAGGERUI
1111
//#define MULTIDOC_OPERATIONtransformer1
1212
//#define OPERATIONtransformer1
13+
//#define OPERATIONtransformer2
1314

1415
#if DEFAULT
1516
// <snippet_default>
@@ -178,6 +179,37 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf
178179
// </snippet_operationtransformer1>
179180
#endif
180181

182+
#if OPERATIONtransformer2
183+
// <snippet_operationtransformer2>
184+
using System.Threading.Tasks;
185+
using Microsoft.AspNetCore.Builder;
186+
using Microsoft.Extensions.DependencyInjection;
187+
using Microsoft.Extensions.Hosting;
188+
189+
var builder = WebApplication.CreateBuilder();
190+
191+
builder.Services.AddOpenApi();
192+
193+
var app = builder.Build();
194+
195+
if (app.Environment.IsDevelopment())
196+
{
197+
app.MapOpenApi();
198+
}
199+
200+
app.MapGet("/old", () => "This endpoint is old and should not be used anymore")
201+
.AddOpenApiOperationTransformer((operation, context, cancellationToken) =>
202+
{
203+
operation.Deprecated = true;
204+
return Task.CompletedTask;
205+
});
206+
207+
app.MapGet("/new", () => "This endpoint replaces /old");
208+
209+
app.Run();
210+
// </snippet_operationtransformer2>
211+
#endif
212+
181213
#if MULTIDOC_OPERATIONtransformer1
182214
// <snippet_multidoc_operationtransformer1>
183215
using Microsoft.AspNetCore.Authentication;

0 commit comments

Comments
 (0)