Skip to content

Commit becb36a

Browse files
committed
Apply all transformers in order instead of last-one-wins
1 parent a9eb1fb commit becb36a

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/OpenApi/src/Services/OpenApiDocumentService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,9 @@ private async Task<Dictionary<OperationType, OpenApiOperation>> GetOperationsAsy
271271

272272
// Apply any endpoint-specific operation transformers registered via
273273
// the AddOpenApiOperationTransformer extension method.
274-
var endpointOperationTransformer = description.ActionDescriptor.EndpointMetadata
275-
.OfType<DelegateOpenApiOperationTransformer>()
276-
.LastOrDefault();
277-
if (endpointOperationTransformer is not null)
274+
var endpointOperationTransformers = description.ActionDescriptor.EndpointMetadata
275+
.OfType<DelegateOpenApiOperationTransformer>();
276+
foreach (var endpointOperationTransformer in endpointOperationTransformers)
278277
{
279278
await endpointOperationTransformer.TransformAsync(operation, operationContext, cancellationToken);
280279
}

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/OperationTransformerTests.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public async Task OperationTransformer_CanAccessTransientServiceFromContextAppli
479479
}
480480

481481
[Fact]
482-
public async Task WithOpenApi_CanApplyTransformer()
482+
public async Task AddOpenApiOperationTransformer_CanApplyTransformer()
483483
{
484484
var builder = CreateBuilder();
485485

@@ -503,7 +503,7 @@ await VerifyOpenApiDocument(builder, document =>
503503
}
504504

505505
[Fact]
506-
public async Task WithOpenApi_TransformerRunsAfterOtherTransformers()
506+
public async Task AddOpenApiOperationTransformer_TransformerRunsAfterOtherTransformers()
507507
{
508508
var builder = CreateBuilder();
509509

@@ -533,6 +533,44 @@ await VerifyOpenApiDocument(builder, document =>
533533
});
534534
}
535535

536+
[Fact]
537+
public async Task AddOpenApiOperationTransformer_SupportsMultipleTransformers()
538+
{
539+
var builder = CreateBuilder();
540+
541+
builder.MapGet("/", () => { })
542+
.AddOpenApiOperationTransformer((operation, context, cancellationToken) =>
543+
{
544+
operation.Description = "Operation Description";
545+
return Task.CompletedTask;
546+
})
547+
.AddOpenApiOperationTransformer((operation, context, cancellationToken) =>
548+
{
549+
operation.Description += " 2";
550+
operation.Deprecated = true;
551+
return Task.CompletedTask;
552+
})
553+
.AddOpenApiOperationTransformer((operation, context, cancellationToken) =>
554+
{
555+
operation.Description += " 3";
556+
operation.OperationId = "OperationId";
557+
return Task.CompletedTask;
558+
});
559+
560+
await VerifyOpenApiDocument(builder, document =>
561+
{
562+
Assert.Collection(document.Paths.OrderBy(p => p.Key),
563+
path =>
564+
{
565+
Assert.Equal("/", path.Key);
566+
var operation = Assert.Single(path.Value.Operations.Values);
567+
Assert.Equal("Operation Description 2 3", operation.Description);
568+
Assert.True(operation.Deprecated);
569+
Assert.Equal("OperationId", operation.OperationId);
570+
});
571+
});
572+
}
573+
536574
private class ActivatedTransformer : IOpenApiOperationTransformer
537575
{
538576
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)