Skip to content

Add No-Op method/extension to OpenApiOptions for interception in inheriting APIs #63225

@TheXenocide

Description

@TheXenocide

Summary

Microsoft.AspNetCore.OpenApi.SourceGenerators uses an interceptor on the AddOpenApi extension method which is only applicable to projects which define their own document generation behavior. Projects which leverage reusable behaviors to establish consistent features across many APIs need a means to allow multiple downstream consumers to append interceptor/generators.

Motivation and goals

Our organization has many solutions and APIs which share common infrastructure/behavior like Auth configuration, caching behaviors, route mapping, shared middleware, Open API doc generation, Swagger UI, etc. We do this via inheritable types and composite extension methods so that we don't have the same exact copy/paste code in a ton of APIs but as a consequence it appears that XML comment -> Open API metadata source generators are not functioning as expected in our projects. Rather than making us rewrite our shared infrastructure in a way that requires duplication or implementing source generators of our own just to leverage this feature it would be ideal if OpenApiOptions could be extended via a no-op method (or extension method) that can be intercepted in addition to the IServiceCollection extension methods to serve as an alternative interception point for the provided source generators.

In scope

One extra interception location or generated extension method for generated Open API metadata methods to integrate with.

Ideally the documentation would also make it more clear what the behavior of these generators is as figuring out what went wrong between POC and implementation was a rather involved process.

Out of scope

Guiding customers to write their own source generators or requiring them to break intentionally composited behavior into a bunch of separate order-specific/sensitive public extensions.

Risks / unknowns

I'm not currently aware of significant risks involved with this approach.

Examples

The goal is to provide a slightly more abstract integration point than just the AddOpenApi(this IServiceCollection ...) extensions to be used by organizations with large/complex shared infrastructure. Something like:

protected override void ConfigureOpenApiDocument(OpenApiOptions options)
{
    base.ConfigureOpenApiDocument(options);

    options.AddGeneratedTransformers();

    // other Solution/Project/API-specific configurations here
}

Or for more extension method chain composition-based implementations:

hostBuilder.UseSharedApiBehaviors(configureOpenApi: options => {
    options.AddGeneratedTransformers();
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcdesign-proposalThis issue represents a design proposal for a different issue, linked in the descriptionfeature-openapi

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions