Skip to content
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

Add APIs for viewing generated OpenAPI file at runtime #54600

Closed
captainsafia opened this issue Mar 18, 2024 · 5 comments
Closed

Add APIs for viewing generated OpenAPI file at runtime #54600

captainsafia opened this issue Mar 18, 2024 · 5 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented 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
Milestone

Comments

@captainsafia
Copy link
Member

captainsafia commented Mar 18, 2024

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<OpenApiOptions> 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

@captainsafia captainsafia added the api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews label Mar 18, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Mar 18, 2024
Copy link
Contributor

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:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@captainsafia captainsafia self-assigned this Mar 18, 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 area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Mar 18, 2024
@captainsafia captainsafia added this to the 9.0-preview4 milestone Mar 18, 2024
@captainsafia captainsafia changed the title Add APIs for view generated OpenAPI file at runtime Add APIs for viewing generated OpenAPI file at runtime Mar 18, 2024
@halter73
Copy link
Member

halter73 commented Mar 18, 2024

API Review:

  • Do we like MapOpenApiJson to be more explicit this doesn't include stuff like Swagger UI?
    • Or MapOpenApiDocuments?
    • Let's stick with MapOpenApi for now, but reserve the right to change it MapOpenApiDocuments in a later preview when we add multi-document support.
  • MapOpenApi should return an IEndpointConventionBuilder
    • It could return a derived type for OpenAPI-specific extension methods similar to MapHub, but we don't think we need it.
  • OpenApiOptions.JsonFilePath shouldn't be called a "file path" since it's really a route
    • JsonRoute? DocumentRoute? JsonDocumentRoute?
      • DocumentRoute is less prescriptive. We might add something like yaml support later.
    • And make it a RoutePattern? Let's not make people call RoutePattern.Parse().
    • Or do we take it as the second parameter to MapOpenApi()? Yes.
  • Where is OpenApiSpecVersion defined? Third party dependency on Microsoft.OpenApi.
    • We think this API is already so inextricably tied to Microsoft.OpenApi that we don't think we lose anything by exposing their types on OpenApiOptions. This puts us in a similar spot we are in with Microsoft.IdentityModel and our JwtBearer/OpenIdConnect/WsFed
  • IEndpointRouteBuilderExtensions and IServiceCollectionExtensions should be renamed to OpenApiEndpointRouteBuilderExtensions and OpenApiServiceCollectionExtensions.
  • We might like an API where you can also specify the version along with MapOpenApi to keep all the configuration in one place and potentially support multiple calls, but that is a more complicated API that might involve a custom IEndopintConventionBuilder
    • Or, if we add no other options before .NET 9, we should consider making OpenApiSpecVersion the last argument to MapOpenApi

API Approved!

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

public static class OpenApiEndpointRouteBuilderExtensions
{
    public static IEndpointConventionBuilder MapOpenApi(this IEndpointRouteBuilder builder, [StringSyntax("Route")] string pattern = "openapi.json");
}

namespace Microsoft.Extensions.DependencyInjection;

public static class OpenApiServiceCollectionExtensions
{
    public static IServiceCollection AddOpenApi(this IServiceCollection serviceCollection);
    public static IServiceCollection AddOpenApi(this IServiceCollection serviceCollection, Action<OpenApiOptions> configureOptions);
}

namespace Microsoft.AspNetCore.OpenApi;

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

@halter73 halter73 added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Mar 18, 2024
@mitchdenny
Copy link
Member

Has anyone ever asked for the ability to split up their open API document into multiple different endpoints? Do we assume that there is only ever one OpenAPI document for the entire app?

@captainsafia
Copy link
Member Author

Has anyone ever asked for the ability to split up their open API document into multiple different endpoints? Do we assume that there is only ever one OpenAPI document for the entire app?

Yes, there's often a need to do multiple documents for different versions of an API or different visibilities (public versus internal).

#54676 outlines how we intend to do this.

@captainsafia
Copy link
Member Author

Closing as this is merged to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented 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

No branches or pull requests

3 participants