-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Introduce Microsoft.AspNetCore.OpenApi package #40676
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
Comments
i really like this idea. |
Also DM's library can translate betwixt versions of OpenAPI (and output format - YAML/JSON). We could use that. |
Sign us up. |
Thanks for contacting us. We're moving this issue to the |
I'm sold 🤑 really like this one. |
Proposed APInamespace Microsoft.AspNetCore.OpenApi;
public static class OpenApiRouteHandlerBuilderExtensions
{
public static RouteHandlerBuilder WithApiDescription(
this RouteHandlerBuilder builder,
Action<OpenApiPathItem>? configureDescription = null) { }
} public static class OpenApiServicesExtensions
{
public static IServiceCollection AddOpenApiGenerator(this IServiceCollection services) {}
} public class OpenApiGenerator
{
public OpenApiGenerator(
IHostEnvironment? environment,
IServiceProviderIsService? serviceProviderIsService) { }
} namespace Microsoft.AspNetCore.Mvc;
public class ProducesResponseTypeAttribute : Attribute, IApiResponseMetadataProvider
{
- internal bool IsResponseTypeSetByDefault { get; }
+ public bool IsResponseTypeSetByDefault { get; }
} Usage Examplesvar builder = WebApplication.CreateBuilder();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiGenerator();
var app = builder.Build();
app.MapGet("/todo/{id}", (int id) => {})
.WithApiDescription(desc => {
desc.Operations[OperationType.Get].Summary = "Get a Todo by ID";
desc.Operations[OperationType.Get].Parameters[0].Summary = "The ID associated with the Todo";
desc.Operations[OperationType.Get].Parameters[0].Required = true;
}); |
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:
|
Some alternatives for
|
Note: WithOpenApi();
WithOpenApi(Func<OpenApiOperation, OpenApiOperation> produceOperation); Remove: public static IServiceCollection AddOpenApiGenerator(this IServiceCollection services) {} Make this type internal: internal class OpenApiGenerator
{
internal OpenApiGenerator(
IHostEnvironment? environment,
IServiceProviderIsService? serviceProviderIsService) { }
} |
API Review Notes:
using Microsoft.AspNetCore.OpenApi;
// ...
app.MapGet("/todos", async (TodoDb db) => await db.Todos.ToListAsync())
.WithOpenApi(operation => new(operation)
{
OperationId = "GetAllTodos",
Description = "This API returns all the Todos.",
})
Final approved API: + namespace Microsoft.AspNetCore.OpenApi;
+
+ public static class OpenApiRouteHandlerBuilderExtensions
+ {
+ public static RouteHandlerBuilder WithOpenApi(this RouteHandlerBuilder builder)
+ public static RouteHandlerBuilder WithOpenApi(this RouteHandlerBuilder builder, Func<OpenApiOperation, OpenApiOperation> configureOperation)
+ }
namespace Microsoft.AspNetCore.Builder;
public abstract class EndpointBuilder
{
+ public IServiceProvider? ServiceProvider { get; set; }
} |
Consider introducing a new package Microsoft.AspNetCore.OpenApi that provides integration features for ASP.NET Core HTTP APIs and OpenAPI. This package takes a dependency on Microsoft.OpenApi and contains the logic that maps primitive ASP.NET Core endpoint details to representations in endpoint metadata using the object model provided by Microsoft.OpenApi, along with projecting these details into the legacy
ApiExplorer
API from MVC. Furthermore, it provides helper methods for easily adding user-specified OpenAPI relevant details directly to endpoint metadata.This would essentially be a strategy shift so that instead of continuing to add more metadata types to the framework to allow more details about endpoint APIs to be declared and published in MVC's
ApiExplorer
(e.g. #40084), we embrace the existing OpenAPI object model in Microsoft.OpenApi and allow all operation details to be specified in an endpoint's metadata and projected intoApiExplorer
so that existing libraries like Swashbuckle continue to work.The package would do the following:
MethodInfo
for a route handler delegate in the endpoint metadata, to the requisiteMicrosoft.OpenApi.Models.OpenApiOperation
Microsoft.OpenApi.Models.OpenApiOperation
in the endpoint metadataIApiDescriptionProvider
that projects a route handler's details from its OpenAPI endpoint metadata intoApiExplorer
We need to consider of course how we do this without breaking (too much) stuff. In .NET 6, Minimal API endpoints are added to the
ApiExplorer
if MVC is configured, or theAddEndpointsApiExplorer()
services extension method is called.The text was updated successfully, but these errors were encountered: