Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support WithOpenApi for registering transformers #60013

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 <see cref="IOpenApiOperationTransformer"/> that targets the given endpoint.
/// </summary>
/// <typeparam name="TBuilder">The type of the endpoint convention builder.</typeparam>
/// <param name="builder">The endpoint convention builder.</param>
/// <param name="transformer">A function that transforms an <see cref="OpenApiOperation"/>.</param>
/// <returns>The endpoint convention builder with the transformer added.</returns>
public static TBuilder WithOpenApi<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.WithOpenApi<TBuilder>(this TBuilder builder, System.Func<Microsoft.OpenApi.Models.OpenApiOperation!, Microsoft.AspNetCore.OpenApi.OpenApiOperationTransformerContext!, System.Threading.CancellationToken, System.Threading.Tasks.Task!>! transformer) -> TBuilder
8 changes: 8 additions & 0 deletions src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ private async Task<Dictionary<OperationType, OpenApiOperation>> GetOperationsAsy
var transformer = operationTransformers[i];
await transformer.TransformAsync(operation, operationContext, cancellationToken);
}

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("/", () => { })
.WithOpenApi((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("/", () => { })
.WithOpenApi((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