Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,17 @@ private static void AddAndConfigureOperationForEndpoint(EndpointBuilder endpoint
}
}
}

/// <summary>
/// Adds an OpenAPI operation transformer to the <see cref="EndpointBuilder.Metadata" /> associated
/// with the current endpoint.
/// </summary>
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
/// <param name="transformer">The <see cref="Func{OpenApiOperation, OpenApiOperationTransformerContext, CancellationToken, Task}"/> that modifies the operation in the <see cref="OpenApiDocument"/>.</param>
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
public static TBuilder AddOpenApiOperationTransformer<TBuilder>(this TBuilder builder, Func<OpenApiOperation, OpenApiOperationTransformerContext, CancellationToken, Task> transformer) where TBuilder : IEndpointConventionBuilder
{
builder.WithMetadata(new DelegateOpenApiOperationTransformer(transformer));
return builder;
}
}
1 change: 1 addition & 0 deletions src/OpenApi/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.AspNetCore.Builder.OpenApiEndpointConventionBuilderExtensions.AddOpenApiOperationTransformer<TBuilder>(this TBuilder builder, System.Func<Microsoft.OpenApi.Models.OpenApiOperation!, Microsoft.AspNetCore.OpenApi.OpenApiOperationTransformerContext!, System.Threading.CancellationToken, System.Threading.Tasks.Task!>! transformer) -> TBuilder
10 changes: 10 additions & 0 deletions src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ private async Task<Dictionary<OperationType, OpenApiOperation>> GetOperationsAsy
var transformer = operationTransformers[i];
await transformer.TransformAsync(operation, operationContext, cancellationToken);
}

// Apply any endpoint-specific operation transformers registered via
// the AddOpenApiOperationTransformer extension method.
var endpointOperationTransformer = description.ActionDescriptor.EndpointMetadata
.OfType<DelegateOpenApiOperationTransformer>()
.LastOrDefault();
if (endpointOperationTransformer is not null)
{
await endpointOperationTransformer.TransformAsync(operation, operationContext, cancellationToken);
}
}
return operations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,61 @@ public async Task OperationTransformer_CanAccessTransientServiceFromContextAppli
Assert.Equal(4, Dependency.InstantiationCount);
}

[Fact]
public async Task WithOpenApi_CanApplyTransformer()
{
var builder = CreateBuilder();

builder.MapGet("/", () => { })
.AddOpenApiOperationTransformer((operation, context, cancellationToken) =>
{
operation.Description = "Operation Description";
return Task.CompletedTask;
});

await VerifyOpenApiDocument(builder, document =>
{
Assert.Collection(document.Paths.OrderBy(p => p.Key),
path =>
{
Assert.Equal("/", path.Key);
var operation = Assert.Single(path.Value.Operations.Values);
Assert.Equal("Operation Description", operation.Description);
});
});
}

[Fact]
public async Task WithOpenApi_TransformerRunsAfterOtherTransformers()
{
var builder = CreateBuilder();

builder.MapGet("/", () => { })
.AddOpenApiOperationTransformer((operation, context, cancellationToken) =>
{
operation.Description = "Operation Description";
return Task.CompletedTask;
});

var options = new OpenApiOptions();
options.AddOperationTransformer((operation, context, cancellationToken) =>
{
operation.Description = "Operation Description 2";
return Task.CompletedTask;
});

await VerifyOpenApiDocument(builder, document =>
{
Assert.Collection(document.Paths.OrderBy(p => p.Key),
path =>
{
Assert.Equal("/", path.Key);
var operation = Assert.Single(path.Value.Operations.Values);
Assert.Equal("Operation Description", operation.Description);
});
});
}

private class ActivatedTransformer : IOpenApiOperationTransformer
{
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
Expand Down
Loading