Nswag: Improve ApiVersioning support by automatically generate routes using Controllers definitions

Created on 9 Aug 2018  路  2Comments  路  Source: RicoSuter/NSwag

Following #1393 and #1355, I'm only suggesting a possible approach for a better API Versioning support.

It would be really useful and less mistake-prone if NSwag could handle the different decleared versions of an API automatically.
Actually, by following #1393, all the different versions of API needs to be registered and configure manually in Startup (version number matching the one in the Controller Attribute, files, etc.).
A possible different approach could be to have something like this:

app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, config =>
{
    /* Looks inside Controllers for all ApiVersion() attributes and generates a route and a JSON file for each one.
     * Also the DefaultUrlTemplate property could be taken from Route() controller attribute, if present.
     */
    config.GeneratorSettings.UseMultipleVersions = true;
    config.SwaggerRoutes.LoadFromControllers();

    // Generic settings for all routes
    config.GeneratorSettings.Title = "My Amazing API";
    config.GeneratorSettings.Description = "Amazing API functionalities.";

    // Specific settings for single route
    foreach (var route in config.SwaggerRoutes)
    {
        route.DefaultUrlTemplate = "{v:apiVersion}/{controller}/{action}/{id?}";
        route.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase;
    }
});

Thank you!

enhancement

Most helpful comment

Some news about that?

All 2 comments

Was this page helpful?
0 / 5 - 0 ratings