Add APIs for viewing generated OpenAPI file at runtime (original) (raw)

Background and Motivation

See #54598 for full background and motivation.

As part of our effort to add built-in support for OpenAPI document generation in the framework, we are adding APIs to register OpenAPI-related services and endpoints in the target user app.

Proposed API

Note: All APIs are net new.

// Assembly: Microsoft.AspNetCore.OpenApi namespace Microsoft.AspNetCore.Builder;

public static class IEndpointRouteBuilderExtensions { public static IEndpointRouteBuilder MapOpenApi(this IEndpointRouteBuilder builder); }

// Assembly: Microsoft.AspNetCore.OpenApi namespace Microsoft.Extensions.DependencyInjection;

public static class IServiceCollectionExtensions { public static IServiceCollection AddOpenApi(this IServiceCollection serviceCollection); public static IServiceCollection AddOpenApi(this IServiceCollection serviceCollection, Action configureOptions); }

// Assembly: Microsoft.AspNetCore.OpenApi namespace Microsoft.AspNetCore.OpenApi;

public class OpenApiOptions { public string JsonFilePath { get; set; } public OpenApiSpecVersion OpenApiVersion { get; set; } }

Usage Examples

var builder = WebApplication.CreateBuilder();

builder.Services.AddOpenApi();

var app = builder.Build();

app.MapOpenApi();

app.MapGet("/", () => "Hello world!");

app.Run();

var builder = WebApplication.CreateBuilder();

builder.Services.AddOpenApi(options => { options.JsonFilePath = "/custom.openapi.json"; });

var app = builder.Build();

app.MapOpenApi();

app.MapGet("/", () => "Hello world!");

app.Run();

Alternative Designs

N/A

Risks

N/A