-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Add support for an interface for OpenAPI operation transformations #56022
Comments
@martincostello Thanks for filing this issue! We intentionally kept the surface area for the transformers API small to start, with the option to introduce an interface for operation transformers if the feedback arose. It appears the feedback has arisen. The API that you've proposed is pretty close in shape to what was originally encapsulated in the API proposal for transformers. The only thing missing is a |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API approved. namespace Microsoft.AspNetCore.OpenApi;
+/// <summary>
+/// Represents a transformer that can be used to modify an OpenAPI operation.
+/// </summary>
+public interface IOpenApiOperationTransformer
+{
+ Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken);
+}
+ public interface IOpenApiSchemaTransformer
+ {
+ Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken);
+ }
public partial sealed class OpenApiOptions
{
+ public OpenApiOptions AddOperationTransformer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TTransformerType>()
+ where TTransformerType : IOpenApiOperationTransformer
+ public OpenApiOptions AddOperationTransformer(IOpenApiOperationTransformer transformer)
- public OpenApiOptions UseTransformer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TTransformerType>()
- where TTransformerType : IOpenApiDocumentTransformer
+ public OpenApiOptions AddDocumentTransformer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TTransformerType>()
+ where TTransformerType : IOpenApiDocumentTransformer
- public OpenApiOptions UseTransformer(IOpenApiDocumentTransformer transformer)
+ public OpenApiOptions AddDocumentTransformer(IOpenApiDocumentTransformer transformer)
- public OpenApiOptions UseTransformer(Func<OpenApiDocument, OpenApiDocumentTransformerContext, CancellationToken, Task> transformer);
+ public OpenApiOptions AddDocumentTransformer(Func<OpenApiDocument, OpenApiDocumentTransformerContext, CancellationToken, Task> transformer);
- public OpenApiOptions UseOperationTransformer(Func<OpenApiOperation, OpenApiOperationTransformerContext, CancellationToken, Task> transformer);
+ public OpenApiOptions AddOperationTransformer(Func<OpenApiOperation, OpenApiOperationTransformerContext, CancellationToken, Task> transformer);
- public OpenApiOptions UseSchemaTransformer(Func<OpenApiSchema, OpenApiSchemaTransformerContext, CancellationToken, Task> transformer);
+ public OpenApiOptions AddSchemaTransformer(Func<OpenApiSchema, OpenApiSchemaTransformerContext, CancellationToken, Task> transformer);
+ public OpenApiOptions AddSchemaTransformer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TTransformerType>()
+ where TTransformerType : IOpenApiSchemaTransformer
+ public OpenApiOptions AddSchemaTransformer(IOpenApiSchemaTransformer transformer);
} |
@captainsafia I'm happy to pick this up, unless you're planning on tackling it yourself. |
@martincostello I'd be happy to review a PR if you're able to open one! 🙇🏽 |
- Add `IOpenApiOperationTransformer`. - Add `IOpenApiSchemaTransformer`. - Rename `Use*Transfomer()` methods to `Add*Transformer()`. Resolves dotnet#56022.
- Add `IOpenApiOperationTransformer`. - Add `IOpenApiSchemaTransformer`. - Rename `Use*Transfomer()` methods to `Add*Transformer()`. Resolves dotnet#56022.
- Add `IOpenApiOperationTransformer`. - Add `IOpenApiSchemaTransformer`. - Rename `Use*Transfomer()` methods to `Add*Transformer()`. Resolves dotnet#56022.
- Add `IOpenApiOperationTransformer`. - Add `IOpenApiSchemaTransformer`. - Rename `Use*Transfomer()` methods to `Add*Transformer()`. Resolves dotnet#56022.
Background and Motivation
In the new OpenAPI functionality in .NET 9 preview.4 there is a first-class interface for document transformations,
IOpenApiDocumentTransformer
, but there isn't an analogous interface for operations.It is possible to perform operation transformations by providing a lambda function, but this can quickly get complicated if you want to do things that are non-trivial, use dependencies from DI etc. compared to if you could encapsulate your logic in a class to perform a specific operation like is possible for document transforms.
Proposed API
These would be implemented the same way the document support is, so that DI works as expected to activate the types, it's AoT-friendly etc.
Usage Examples
Alternative Designs
Make the code that enumerates the operations re-usable to public callers so that if an operation interface isn't desired, then the user can implement a document transformer to easily enumerate the operations to do a transform in their custom implementation of the interface. Maybe a base class that implements
IOpenApiDocumentTransformer
and applies the visitor pattern to allow the user to override methods to apply transforms:Risks
Increased complexity of the OpenAPI generator implementation.
The text was updated successfully, but these errors were encountered: