Skip to content

Commit eda87e2

Browse files
committed
Fix description of transformer execution order
1 parent 6a9696a commit eda87e2

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

aspnetcore/fundamentals/openapi/customize-openapi.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,23 @@ Transformers can be registered onto the document by calling the <xref:Microsoft.
4242

4343
### Execution order for transformers
4444

45-
Transformers execute in first-in first-out order based on registration. In the following snippet, the document transformer has access to the modifications made by the operation transformer:
46-
47-
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_transInOut&highlight=3-9)]
45+
Transformers are executed as follows:
46+
47+
* Schema transformers are executed when a schema is register to the document. Schema transformers are executed in the order in which they were added.
48+
All schemas are added to the document before any operation processing occurs, so all schema transformers are executed before any operation transformers.
49+
* Operation transformers are executed when an operation is added to the document. Operation transformers are executed in the order in which they were added.
50+
All operations are added to the document before any document transformers are executed.
51+
* Document transformers are executed when the document is generated. This is the final pass over the document, and all operations and schemas have been add by this point.
52+
* When an application is configured to generate multiple OpenAPI documents, each document is generated in the order in which it was registered, and the transformers are executed as documents are generated.
53+
54+
For example, in the following snippet:
55+
* SchemaTransformer2 is executed has access to the modifications made by SchemaTransformer1.
56+
* Both OperationTransformer1 and OperationTransformer2 have access to the modifications made by both schema transformers for the types involved in the operation they are called to process.
57+
* OperationTransformer2 is executed after OperationTransformer1, so it has access to the modifications made by OperationTransformer1.
58+
* Both DocumentTransformer1 and DocumentTransformer2 are executed after all operations and schemas have been added to the document, so they have access to all modifications made by the operation and schema transformers.
59+
* DocumentTransformer2 is executed after DocumentTransformer1, so it has access to the modifications made by DocumentTransformer1.
60+
61+
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_transInOut&highlight=6-14)]
4862

4963
## Use document transformers
5064

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//#define DEFAULT
2-
//#define DOCUMENTtransformerInOut
2+
#define DOCUMENTtransformerInOut
33
//#define DOCUMENTtransformer1
44
//#define DOCUMENTtransformer2
5-
#define DOCUMENTtransformerUse999
5+
// #define DOCUMENTtransformerUse999
66
//#define FIRST
77
//#define OPENAPIWITHSCALAR
88
//#define MAPOPENAPIWITHCACHING
@@ -296,7 +296,7 @@ public class Body {
296296
if (app.Environment.IsDevelopment())
297297
{
298298
app.MapOpenApi();
299-
299+
300300
app.UseSwaggerUI(options =>
301301
{
302302
options.SwaggerEndpoint("/openapi/v1.json", "v1");
@@ -484,14 +484,19 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
484484

485485
#if DOCUMENTtransformerInOut
486486
// <snippet_transInOut>
487+
using Microsoft.AspNetCore.OpenApi;
488+
using Microsoft.OpenApi.Models;
489+
487490
var builder = WebApplication.CreateBuilder();
488491

489492
builder.Services.AddOpenApi(options =>
490493
{
491-
options.AddOperationTransformer((operation, context, cancellationToken)
492-
=> Task.CompletedTask);
493-
options.AddDocumentTransformer((document, context, cancellationToken)
494-
=> Task.CompletedTask);
494+
options.AddDocumentTransformer<DocumentTransformer1>();
495+
options.AddSchemaTransformer<SchemaTransformer1>();
496+
options.AddDocumentTransformer<DocumentTransformer2>();
497+
options.AddOperationTransformer<OperationTransformer1>();
498+
options.AddSchemaTransformer<SchemaTransformer2>();
499+
options.AddOperationTransformer<OperationTransformer2>();
495500
});
496501

497502
var app = builder.Build();
@@ -506,4 +511,34 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
506511
app.Run();
507512
// </snippet_transInOut>
508513

514+
internal class DocumentTransformer1 : IOpenApiDocumentTransformer
515+
{
516+
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) => Task.CompletedTask;
517+
}
518+
519+
internal class DocumentTransformer2 : IOpenApiDocumentTransformer
520+
{
521+
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) => Task.CompletedTask;
522+
}
523+
524+
internal class OperationTransformer1 : IOpenApiOperationTransformer
525+
{
526+
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) => Task.CompletedTask;
527+
}
528+
529+
internal class OperationTransformer2 : IOpenApiOperationTransformer
530+
{
531+
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) => Task.CompletedTask;
532+
}
533+
534+
internal class SchemaTransformer1 : IOpenApiSchemaTransformer
535+
{
536+
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken) => Task.CompletedTask;
537+
}
538+
539+
internal class SchemaTransformer2 : IOpenApiSchemaTransformer
540+
{
541+
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken) => Task.CompletedTask;
542+
}
543+
509544
#endif

0 commit comments

Comments
 (0)