fcatae/Arda

Create API versioning with Swagger support

fcatae opened this issue · 3 comments

See this discussion: dotnet/aspnet-api-versioning#60

Solution:

  1. Define the available versions
c.SwaggerDoc("v1", new Info { Title = "Arda.Kanban", Version = "v1" });
c.SwaggerDoc("v2", new Info { Title = "Arda.Kanban v2 (Workspaces)", Version = "v2" });
  1. Define a function to classify the API in version
c.DocInclusionPredicate((docName, apiDesc) =>
{
    switch (docName)
    {
        case "v1":
            return apiDesc.RelativePath.StartsWith("api/");
        case "v2":
            return apiDesc.RelativePath.StartsWith("v2/");
    }

    // unknown version?
    return true;
});
  1. Update the Swagger UI
app.UseSwaggerUi(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Arda.Kanban v1");
    c.SwaggerEndpoint("/swagger/v2/swagger.json", "Arda.Kanban v2 (Workspaces)");
});

Done