-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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 registering OpenApiOperationTransformer via extension method for Minimal APIs #59180
Comments
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 review notes?
We'd like to know if we can make the existing |
Bringing this back in for review after some more iteration. I've revised the API name to make it a little harder to associate this with the existing overloads. |
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:
|
// Assembly: Microsoft.AspNetCore.OpenApi
namespace Microsoft.AspNetCore.Builder;
public static class OpenApiEndpointConventionBuilderExtensions
{
public static TBuilder AddOpenApiOperationTransformer<TBuilder>(this TBuilder builder, Func<OpenApiOperation, OpenApiOperationTransformerContext, CancellationToken, Task> transformer) where TBuilder : IEndpointConventionBuilder { }
} |
Hi @captainsafia, I watched your ASP.NET API Review on youtube this morning where you discussed this issue. I personally think you guys do handle the situation WithOpenApi() a bit too easily (with the best of intentions). As I pointed out in my issue #59812. But also mentioned in #59927 of @sander1095 , we are currently being led astray by various forces. So what is the problem with making it obsolete? How long can that marker stay there? Is there no migration path for Swashbuckle then? So at least link some kind of change to this issue to naturally lead people in the right direction. Some remark down in the docs is not enough in my opinion. To me, creating sample messages so they can be fired directly into the generated client is very obvious and part of creating an API. I assume more people do that. It's not enjoyable that you are now so easily led in the wrong direction. |
Background and Motivation
I'm opening this proposal after a conversation with @sander1095 in relation to #58723 and #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.// Assembly: Microsoft.AspNetCore.OpenApi namespace Microsoft.AspNetCore.Builder; public static class OpenApiEndpointConventionBuilderExtensions { + public static TBuilder AddOpenApiOperationTransformer<TBuilder>(this TBuilder builder, Func<OpenApiOperation, OpenApiOperationTransformerContext, CancellationToken, Task> transformer) where TBuilder : IEndpointConventionBuilder { } }
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 #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
Addendum
In the first round of reviews on this API, there was feedback about whether it was feasible to reuse the existing
WithOpenApi
to fulfill this feature. After some investigation, a new API is required because:WithOpenApi
overloads follow last-one-wins semantics but we want this API to be additive.The text was updated successfully, but these errors were encountered: