Open captainsafia opened 1 day ago
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:
Background and Motivation
I'm opening this proposal after a conversation with @sander1095 in relation to https://github.com/dotnet/aspnetcore/issues/58723 and https://github.com/dotnet/aspnetcore/issues/58724.
Prior to .NET 9, we supported a
WithOpenApi
extension method on that when invoked would generate anOpenApiOperation
, inject it into endpoint metadata, then rely on consuming OpenAPI implementations like Swashbuckle to pluck thisOpenApiOperation
and integrate it into the document that was being generated.When we introduced built-in OpenAPI support in .NET 9, we opted not to bring in support for this strategy and instead steer people towards the new
IOpenApiOperationTransformer
abstraction for making modifications to their OpenAPI document.However, one of the things that @sander1095 pointed out with this approach is that you lose the ability to have operation transformations colocated with the endpoint they affected. For example, let's say that I want to set the description for a response in a given endpoint. With the operation transformer model, I might have to write something like this:
In addition to the transformer being far away from the associated endpoint, I also have to implement the associated path-based check myself.
Proposed API
This issue proposes introducing a
WithOpenApiTransformer
extension method that can be used to register an operation transformer for a given endpoint without having to use the global registration feature.Usage Examples
With this proposed API, the same example above could be re-implemented in the following way:
There's also the option for 3rd party authors to provide their own extensions on top of this API to support further customizations. For example, the support for setting descriptions on responses as proposed in https://github.com/dotnet/aspnetcore/issues/58724 can be implemented in the following way:
With the consumption pattern in the invoked code being:
Alternative Designs
WithOpenApi
overloads. However, this would prevent users from being able to access theOpenApiOperationTransformerContext
to customize the behavior of the transformer.Risks
WithOpenApi
andWithOpenApiTransformer
APIs. We can consider naming the new overload toWithOpenApi
to mitigate this...although that might be even more confusing.