domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.26k stars 1.32k forks source link

[Question]: any way to make a "vnext" and/or "current" version api #3131

Closed HugoVG closed 3 weeks ago

HugoVG commented 3 weeks ago

What are you wanting to achieve?

I want to make a /swagger/current/swagger.json or /swagger/current/swagger.yaml with the last APIVersion from Asp.Versioning

currently It's making

but when I try to add a document based on "v2.1" or "v2" it will render with empty paths I think this is case some people already have had before and I need a pointer where to look

What code or approach do you have so far?

builder.Services.AddApiVersioning(options =>
{
    options.DefaultApiVersion = new ApiVersion(2.1); // <-- for v2.1
    options.ReportApiVersions = true;
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.ApiVersionReader = ApiVersionReader.Combine(
        new UrlSegmentApiVersionReader());
    options.UnsupportedApiVersionStatusCode = StatusCodes.Status400BadRequest;
})
.AddMvc()
.AddApiExplorer(options =>
{
    options.GroupNameFormat = "'v'VVV";
    options.SubstituteApiVersionInUrl = true;
});
 List<string> versions = ["v1", "v2", "v2.1"];
foreach (var version in versions)
{
    options.SwaggerDoc(version, new OpenApiInfo { Title = $"API {version.ToUpper()}", Version = version });
}

var latestVersion = versions.Last();
options.SwaggerDoc("current", new OpenApiInfo { Title = "Current Version", Version = latestVersion }); // Creates empty paths
openapi: 3.0.1
info:
  title: Current Version
  version: v2.1 
servers:
  - url: https://localhost:7114
    description: Local server
  - url: https://--
    description: Test server
  - url: https://--
    description: Production server
paths: { }
components:
  securitySchemes:
    Bearer:
      type: apiKey
      description: 'The Token to access restricted content, request a token at /api/Authorization/GetAuthApiKey'
      name: Authorization
      in: header
security:
  - Bearer: [ ]

Additional context

No response

martincostello commented 3 weeks ago

Could you just make endpoint that is a redirect?

List<string> versions = ["v1", "v2", "v2.1"];
// ...
app.MapGet("/swagger/current/swagger.json", () =>
{
    return Results.Redirect($"/swagger/{versions.Last()}/swagger.json");
});
HugoVG commented 3 weeks ago

I'm really stupid and haven't thought about that.