-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Conversation
src/OpenApi/src/Transformers/OpenApiSchemaTransformerContext.cs
Outdated
Show resolved
Hide resolved
src/OpenApi/src/Transformers/OpenApiSchemaTransformerContext.cs
Outdated
Show resolved
Hide resolved
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 = []; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There is |
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 |
eceb7a5
to
62c84cd
Compare
There was a problem hiding this 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.
src/OpenApi/src/Transformers/OpenApiSchemaTransformerContext.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
We discussed the similarities/differences between transformers and middleware/filters. Looking back at the notes, it doesn't look like we discussed I have a slight preference towards |
This PR adds support for the OpenAPI schema transformers API to the built-in generator.
Schema Transformer:
src/OpenApi/src/Services/OpenApiOptions.cs
: Added a newUseSchemaTransformer
method that allows users to register a delegate as a schema transformer. This method adds the provided transformer delegate to theSchemaTransformers
list. [1] [2]src/OpenApi/perf/Microbenchmarks/TransformersBenchmark.cs
: A newSchemaTransformer_Setup
method has been added. This method sets up a new schema transformer that adds an extension to the schema based on the context. A new benchmark method,SchemaTransformer
, has also been added. [1] [2]Additional Changes:
src/OpenApi/src/PublicAPI.Unshipped.txt
: Added new entries related to theOpenApiSchemaTransformerContext
class and theUseSchemaTransformer
method in theOpenApiOptions
class.src/OpenApi/perf/Microbenchmarks/TransformersBenchmark.cs
: Imported theMicrosoft.OpenApi.Any
namespace.