Buccaneer_SwaggerGen 1.0.0

dotnet add package Buccaneer_SwaggerGen --version 1.0.0
NuGet\Install-Package Buccaneer_SwaggerGen -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Buccaneer_SwaggerGen" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Buccaneer_SwaggerGen --version 1.0.0
#r "nuget: Buccaneer_SwaggerGen, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Buccaneer_SwaggerGen as a Cake Addin
#addin nuget:?package=Buccaneer_SwaggerGen&version=1.0.0

// Install Buccaneer_SwaggerGen as a Cake Tool
#tool nuget:?package=Buccaneer_SwaggerGen&version=1.0.0

Buccaneer

A netcore3.1 generator for Swagger (OpenAPI) Specs that use OAuth2 and OpenIDConnect for authentication, without the fuss and dependencies of Swashbuckle.

Fast and simple (<600 loc) and it's only dependency is the Microsoft OpenAPI library package.

Consists only of a few methods and a couple of decorator attributes, so it's ultra simple to get started.

Usage :

Install it from Nuget (Package name Buccaneer_SwaggerGen) or grab the single source code file from here.

  1. Generate a swagger defintiion in YAML from inside C# code for an API using Azure AD B2C to authenticate via the implicit flow.

         return new Buccaneer.SwaggerGenerator().GetSwaggerAsHttpResponseMessageWithYAMLString(
             _ass: Assembly.GetExecutingAssembly(),
             _title: "Wills Blog API",
             _apiversion: "0.1",
             _description: "A sample blog API",
             _contactname: "Will Eastbury",
             _contactemail: "willeastbury@gmail.com",
             _apiServerPaths: new List<string>() { "http://localhost:7071/api", "https://www.willeastbury.com/api" },
             _oauth2scopes: new Dictionary<string, string> { { "https://threeshadesb2c.onmicrosoft.com/willeastburycom/connect", "Connect" }},
             _headerapikeyname: null,
             _oauth2AuthUrl: "https://sample.b2clogin.com/threeshadesb2c.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_willeastbury.com",
             _securitySchemeName: "oauth2"
             );
     }
    

2a. Somewhere in your assembly, you need to decorate something with one of the following attributes, this can be a concrete class OR an interface (Unlike Swashbuckle, this doesn't have to be a asp.net Web api call).

Methods can be decorated with TWO attributes

  • OpenApiTagInputParameterAttribute() which tells the engine which class your method expects to receive and we will serialize that to the output, applying recursion for nested types.

  • OpenApiTagMethodAttribute() This tells the engine that you intend to expose this method over swagger, simply add this attribute to the methods that you want to expose.

Here's a sample that exposes an interface with no concrete implementation, which is awesome for mocking an API, or for example building a swagger for a service that doesn't use c#, like a logic app, or a facade implemented in Azure Functions Proxies.

public interface ITenantAPI
{
    [OpenApiTagMethod("Tenant", "Add Tenant", "Adds a BlogTenant to the platform", new int[] { 200, 404 }, "BlogTenant", "oauth2", "", route: "Tenants", methodlist: "post")]
    public Task<IActionResult> AddTenant();

    [OpenApiTagInputParameter("tenantId", true, "string", "Path")]
    [OpenApiTagMethod("Tenant", "Delete Tenant", "Deletes a BlogTenant from the platform", new int[] { 200, 404 }, "BlogTenant", "oauth2", "", route: "Tenants/{tenantId}", methodlist: "delete")]
    public Task<IActionResult> DeleteTenant();

}

2b. Here's an example of generating a swagger for an http bound Azure Function (and yes it works with netcore 3.1 and the new v3 functions runtime) behind AAD easyauth with an AAD B2C domain.

using static Buccaneer.SwaggerGenerator;

namespace Threeshades_Blog_Engine { public class Categories {

    [FunctionName("GetCategoryData")]
    [OpenApiTagMethod("Category", "Get Category Data", "Retrieves categories on a blog", new int[] { 200, 409 }, "ListOfCategories", null, null, route: "{tenantId}/Categories", methodlist: "get")]
    [OpenApiTagInputParameter("tenantId", true, "string", "Path")]
    public static async Task<IActionResult> GetCategoryData(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "{tenantId:alpha}/Categories")] HttpRequest req,
        [Blob("categories-{tenantId}", FileAccess.ReadWrite)] CloudBlobContainer cbc,
        string tenantId,
        ILogger log)
    {

        log.LogInformation($"C# HTTP trigger function processed a request. for categories for {tenantId}");
        CloudBlockBlob cbb = cbc.GetBlockBlobReference($"categorylist-{tenantId}.blob");
        return (ActionResult)new OkObjectResult(await cbb.DownloadTextAsync());

    }

} }

And boom, you're done, it really is that simple !

Instant Swagger Definition, you can also call GetSwaggerAsJSONString() if you want a JSON swagger instead of a YAML one.

  1. This sample will even host the Swagger Definition INSIDE your function app, if you want a self-swagger-documenting Azure Function App

using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.Extensions.Logging; using System.Collections.Generic; using System.Net.Http; using System.Reflection;

namespace Threeshades_Blog_Engine { public class GenerateSwaggerFunction {

    [FunctionName("GenerateSwaggerFunction")]
    public HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "swagger")] HttpRequestMessage req, ILogger log)
    {
        return new Buccaneer.SwaggerGenerator().GetSwaggerAsHttpResponseMessageWithYAMLString(
            _ass: Assembly.GetExecutingAssembly(),
            _title: "Wills Blog API",
            _apiversion: "0.1",
            _description: "A sample blog API",
            _contactname: "Will Eastbury",
            _contactemail: "willeastbury@gmail.com",
            _apiServerPaths: new List<string>() { "http://localhost:7071/api", "https://www.willeastbury.com/api" },
            _oauth2scopes: new Dictionary<string, string> { { "https://threeshadesb2c.onmicrosoft.com/willeastburycom/connect", "Connect" }},
            _headerapikeyname: null,
            _oauth2AuthUrl: "https://threeshadesb2c.b2clogin.com/threeshadesb2c.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_willeastbury.com",
            _securitySchemeName: "oauth2"
            );
    }
}

}

This will expose a /swagger endpoint as an http triggered Azure Function.

Enjoy !

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 477 1/27/2020