I'm working on a project using the Clean Architecture Template.
I'm trying to configure the Endpoints with OpenApi specifications.
Here's what I tried to implement:
Custom MapGet Extension
public static class IEndpointRouteBuilderExtensions
{
public static IEndpointRouteBuilder MapGet(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
{
Guard.Against.AnonymousMethod(handler);
builder.MapGet(pattern, handler)
.WithName(handler.Method.Name)
.WithOpenApi();
return builder;
}
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.AddOpenApi();
services.AddEndpointsApiExplorer();
var app = builder.Build();
app.UseHttpsRedirection();
app.MapEndpoints(); // Maps endpoints via reflection
app.MapOpenApi(); // OpenAPI support
app.Run();
WebApplicationExtensions.cs
public static class WebApplicationExtensions
{
public static RouteGroupBuilder MapGroup(this WebApplication app, EndpointGroupBase group)
{
var groupName = group.GetType().Name;
return app
.MapGroup($"/api/{groupName}")
.WithGroupName(groupName)
.WithTags(groupName)
.WithOpenApi();
}
public static WebApplication MapEndpoints(this WebApplication app)
{
var endpointGroupType = typeof(EndpointGroupBase);
var assembly = Assembly.GetExecutingAssembly();
var endpointGroupTypes = assembly.GetExportedTypes()
.Where(t => t.IsSubclassOf(endpointGroupType));
foreach (var type in endpointGroupTypes)
{
if (Activator.CreateInstance(type) is EndpointGroupBase instance)
{
instance.Map(app);
}
}
return app;
}
}
Sample Endpoint Group: WeatherForecasts
public class WeatherForecasts : EndpointGroupBase
{
public override void Map(WebApplication app)
{
app.MapGroup(this)
.MapGet(GetWeatherForecasts);
}
public async Task<Ok<IEnumerable<WeatherForecast>>> GetWeatherForecasts([AsParameters] WeatherService services)
{
var forecasts = await services.Queries.GetWeatherForecastsAsync();
return TypedResults.Ok(forecasts);
}
}
The problem is: the endpoint does not appear in the OpenAPI JSON (https://localhost:7283/openapi/v1.json).
There are no errors, but the custom MapGet extension seems to silently fail or not register the route properly. The endpoint is working if I test it directly via its URL, but it's invisible to OpenAPI.
But when i try in this way:
app.MapGet("/test", ([FromServices] IWeatherForecastsQueries queries) =>
{
return Results.Ok(new[] { new WeatherForecast() });
}).WithOpenApi();
It appears in a OpenApi specifications.
Why is the endpoint not appearing in the OpenAPI spec when using my custom MapGet extension method?
.WithGroupNamewhen defining group. It seems it breaks exploration