Skip to content

Add support for OpenAPI schema transformers #56093

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

Merged
merged 3 commits into from
Jun 8, 2024

Conversation

captainsafia
Copy link
Member

This PR adds support for the OpenAPI schema transformers API to the built-in generator.

Schema Transformer:

Method TransformerCount Mean Error StdDev Op/s Gen 0 Gen 1 Gen 2 Allocated
SchemaTransformer 10 10.01 us 0.096 us 0.085 us 99,938.4 0.1221 - - 20 KB
SchemaTransformer 100 13.01 us 0.141 us 0.110 us 76,890.6 0.1831 - - 25 KB
SchemaTransformer 1000 40.00 us 0.289 us 0.242 us 25,002.0 0.4883 - - 82 KB

Additional Changes:

@captainsafia captainsafia requested a review from a team as a code owner June 6, 2024 05:35
@ghost ghost added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jun 6, 2024
@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jun 6, 2024
@davidfowl
Copy link
Member

Can these transformers access the IServiceProvider?

@@ -14,6 +14,7 @@ namespace Microsoft.AspNetCore.OpenApi;
public sealed class OpenApiOptions
{
internal readonly List<IOpenApiDocumentTransformer> DocumentTransformers = [];
internal readonly List<Func<OpenApiSchema, OpenApiSchemaTransformerContext, CancellationToken, Task>> SchemaTransformers = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are scenarios where someone could have an async schema transformer?

Copy link
Member

@JamesNK JamesNK Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is async reading an XML comments file from disk and then applying changes to the schema an example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XML comments support will be handled separately in this implementation, although theoretically there's nothing stopping someone from rolling out an implementation on their own or reading additional metadata from some non-XML file.

@JamesNK
Copy link
Member

JamesNK commented Jun 6, 2024

Can these transformers access the IServiceProvider?

There is OpenApiSchemaTransformerContext.ApplicationServices.

@captainsafia
Copy link
Member Author

Can these transformers access the IServiceProvider?

In addition to the property on the context object @JamesNK mentioned above, we also have a generic API for supporting resolving activated instances of a transformer OpenApiOptions.UseTransformer<T>. To keep the API small, we've only exposed it for document transformers at the moment but it's a pattern we can expand to other transformers as the need arises (e.g. there's an API proposal to add it. for operation transformers).

@captainsafia captainsafia force-pushed the safia/schema-transformers branch from eceb7a5 to 62c84cd Compare June 6, 2024 16:04
Copy link
Member

@BrennanConroy BrennanConroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Should add a test for checking DocumentName and ApplicationServices.

Copy link
Member

@halter73 halter73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we discuss Use...Transformer vs Add...Transformer in API review? I only ask because we went with AddEndpointFilter over UseEndpointFilter in .NET 7, and this feels even more addy so-to-speak since there can be multiples of each type of transformer and there's no next callback unlike filters and middleware.

@captainsafia
Copy link
Member Author

Did we discuss Use...Transformer vs Add...Transformer in API review? I only ask because we went with AddEndpointFilter over UseEndpointFilter in .NET 7, and this feels even more addy so-to-speak since there can be multiples of each type of transformer and there's no next callback unlike filters and middleware.

We discussed the similarities/differences between transformers and middleware/filters. Looking back at the notes, it doesn't look like we discussed Use vs Add specifically.

I have a slight preference towards Add myself. We can discuss this rename at the upcoming review around adding an interface type for operation transformers.

@captainsafia captainsafia enabled auto-merge (squash) June 7, 2024 23:28
@captainsafia captainsafia merged commit 206b0ae into main Jun 8, 2024
26 checks passed
@captainsafia captainsafia deleted the safia/schema-transformers branch June 8, 2024 02:04
@dotnet-policy-service dotnet-policy-service bot added this to the 9.0-preview6 milestone Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants