Giter Site home page Giter Site logo

bungeemonkee / swashbuckleaspnetversioningshim Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rh072005/swashbuckleaspnetversioningshim

0.0 1.0 0.0 36 KB

Library to aid the combined use of Swashbuckle and ASP NET API Versioning

License: MIT License

C# 77.33% PowerShell 15.31% Shell 7.36%

swashbuckleaspnetversioningshim's Introduction

Swashbuckle ASP.NET Versioning Shim

Build status

This library aids the use of Swashbuckle and ASP NET Web API Versioning together and started from my attempt at resolving Swashbuckle.AspNetCore issue 244

Note: Development has been carried out with URL path versioning in mind. I've not tested it with the other versioning conventions that API versioning provide.

Getting started

  • Start by creating a new ASP.NET Core Web Application
  • Install the SwashbuckleAspNetVersioningShim NuGet package
    • Short term this will be a case of building from source
    • Mid term it will be hosted on MyGet Now available on MyGet
    • Long term it will be hosted on NuGet Now available on NuGet
  • Add the following code blocks to Startup.cs
using SwashbuckleAspNetVersioningShim;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
public void ConfigureServices(IServiceCollection services)
{
    // This replaces services.AddMvc(); because we need access to IMvcBuilder's ApplicationPartManager below
    var mvcBuilder = services.AddMvc();

    services.AddApiVersioning();
    services.AddSwaggerGen(c =>
    {
        SwaggerVersioner.ConfigureSwaggerGen(c, mvcBuilder.PartManager);
    });
    ...
//Note the change of method signature to include injection of ApplicationPartManager
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationPartManager partManager)
{
    ...
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        SwaggerVersioner.ConfigureSwaggerUI(c, partManager);
    });
    ...
}

All being well you can now continue to use ASP NET Web API Versioning as per it's documentation. As a minimum your web API controller will want an ApiVersionAttribute and a RouteAttribute. Using the default ValueController that's created with a new Web API project it would look like this

[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
    ...

Note about MapToApiVersion

When using MapToApiVersion (example here) methods will be added for each ApiVersionAttribute specified on the controller. In the example this results in Get() and GetV3() being added to the 2.0 and 3.0 Swagger document. To avoid this, which will cause an overload error in Swashbuckle, you will need to add explicitly add the MapToApiVersion attribute to both methods rather than letting Get() default.

Referring again to the example it would look like this

[ApiVersion("2.0")]
[ApiVersion("3.0")]
[Route("api/v{version:apiVersion}/helloworld")]
public class HelloWorld2Controller : Controller
{
    [HttpGet, MapToApiVersion("2.0")]
    public string Get() => "Hello world v2!";

    [HttpGet, MapToApiVersion("3.0")]
    public string GetV3() => "Hello world v3!";
}

License

See the LICENSE file for license rights and limitations (MIT).

swashbuckleaspnetversioningshim's People

Contributors

ryan-bennetts avatar rh072005 avatar bungeemonkee avatar paddoswam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.