nancy-metadata-openapi - Nancy.Metadata.OpenApi 0.11.0

A library to add capabilities to a NancyFx web service to generate a json doc that complies to OpenApi spec 3.0.2.

PM> Install-Package Nancy.Metadata.OpenApi -Version 0.11.0 -Source https://www.myget.org/F/nancy-metadata-openapi/api/v3/index.json

Copy to clipboard

> nuget.exe install Nancy.Metadata.OpenApi -Version 0.11.0 -Source https://www.myget.org/F/nancy-metadata-openapi/api/v3/index.json

Copy to clipboard

> dotnet add package Nancy.Metadata.OpenApi --version 0.11.0 --source https://www.myget.org/F/nancy-metadata-openapi/api/v3/index.json

Copy to clipboard
<PackageReference Include="Nancy.Metadata.OpenApi" Version="0.11.0" />
Copy to clipboard
source https://www.myget.org/F/nancy-metadata-openapi/api/v3/index.json

nuget Nancy.Metadata.OpenApi  ~> 0.11.0
Copy to clipboard

> choco install Nancy.Metadata.OpenApi --version 0.11.0 --source https://www.myget.org/F/nancy-metadata-openapi/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "nancy-metadata-openapi" -SourceLocation "https://www.myget.org/F/nancy-metadata-openapi/api/v2"
Install-Module -Name "Nancy.Metadata.OpenApi" -RequiredVersion "0.11.0" -Repository "nancy-metadata-openapi" 
Copy to clipboard

Nancy.Metadata.OpenApi Mit License

This library extends Nancy modules in order to produce a specification file that will follow the OpenAPI spec.

You can find the latest specifications of OpenAPI here

This library targets Nancy 2.0, for previous versions that target Nancy 1.X check release that target version 0.7.0 and below.

Note: Since Nancyfx is no longer being maintained i will be deprioritizing this library, please read here for details

Builds

Appveyor Branch Coverage
Build status master CodeCov
Build status develop CodeCov

Packages

NuGet (Stable) MyGet (Prerelease)
NuGet MyGet

Installation

Via nuget:

PM> Install-Package Nancy.Metadata.OpenApi

You also need the Metadata library provided by Nancyfx:

PM> Install-Package Nancy.Metadata.Modules

Usage

Define a docs module that will serve our OpenApi Json (currently only json is supported) document:

using Nancy.Metadata.OpenApi.Model;
using Nancy.Metadata.OpenApi.Modules;

public class DocsModule : OpenApiDocsModuleBase //We must inherit from the OpenApiDocsModuleBase
{
    //Could be an array of Servers corresponding to different environments. 
    public static Server Server
        => new Server() { Description = "My Descripton", Url = "http://localhost:5000/" };

    public static string[] Tags => new string[] { "sample", "openapi" };

    public DocsModule(IRouteCacheProvider routeCacheProvider) :
        base(routeCacheProvider,
        "/api/docs/openapi.json",       //Document location path
        "My API ",                      //Api Title 
        "v1.0",                         //Version of the Api
        Server,
        "/api",                         //Base url
        Tags)                           //Document Tags
    {
    }
}

We could optionally, if the information is needed, add Contact, License and External Docs information:

public class DocsModule : OpenApiDocsModuleBase //We must inherit from the OpenApiDocsModuleBase
{
    public static Server Server
        => new Server() { Description = "My Descripton", Url = "http://localhost:5001/" };

    public static string[] Tags => new string[] { "sample", "openapi" };

    public DocsModule(IRouteCacheProvider routeCacheProvider) :
        base(routeCacheProvider, "/api/docs/openapi.json", "My API 2", "v1.1", Server, "/api", Tags)
    {
        //Optional information.
        WithContact("Contact Information", "jaxelrojas@email.com", "https://jaxelr.github.io");

        //Optional information.
        WithLicense("MIT", "https://opensource.org/licenses/MIT");

        //Optional Information.
        WithExternalDocument("This is a tutorial or a spec doc.", "https://jaxelr.github.io")
    }
}

Then, define the Nancy modules as you would usually do:


//Example using Nancy v2
public class MyModule : NancyModule
{
    public MyModule() : base("/api")
    {
        Get("/hello", r => HelloWorld(), name: "SimpleRequest");
        Get("/hello/{name}", r => Hello(r.name), name: "SimpleRequestWithParameter");
    }
}

//Skipped method implementations for brevity sake...

Finally, you must define the metadata of the operations. To do so, simply declare the metadata module (using Nancy.Metadata.Modules) on the same namespace as the endpoint operations were defined, using the inherited MetadataModule class and the OpenApiRouteMetadata class defined on Nancy.Metadata.OpenApi.Core.

using Nancy.Metadata.Modules;
using Nancy.Metadata.OpenApi.Core;
using Nancy.Metadata.OpenApi.Fluent;

public class MyMetadataModule : MetadataModule<OpenApiRouteMetadata>
{
    public MyMetadataModule()
    {
        Describe["SimpleRequest"] = desc => new OpenApiRouteMetadata(desc)
            .With(i => i.WithResponseModel("200", typeof(SimpleResponseModel), "Sample response")
                        .WithSummary("Simple GET example"));

        Describe["SimpleRequestWithParameter"] = desc => new OpenApiRouteMetadata(desc)
            .With(i => i.WithResponseModel("200", typeof(SimpleResponseModel), "Sample response")
                        .WithRequestParameter("name")
                        .WithSummary("Simple GET with parameters"));
    }
}

Thats pretty much it, the docs endpoint defined above would generate some valid OpenApi Json. You can validate the Open Api endpoint using swagger-ui. (For those unaware, OpenApi used to be called Swagger, so any reference to Swagger usually means version <= 2.0) Check the Compatibility table of UI for usage.

For a working example, see the sample app that uses the Swagger-UI site as a validator.

Contributing

Check the guidelines for a simple explanation on how you could contribute.

Adding new features related to standard 3.0.2

  • .NETFramework 4.5.2
    • Nancy (>= 2.0.0)
    • Newtonsoft.Json (>= 12.0.2)
    • System.ValueTuple (>= 4.5.0)
  • .NETStandard 2.0
    • Nancy (>= 2.0.0)
    • Newtonsoft.Json (>= 12.0.2)
    • System.ValueTuple (>= 4.5.0)
  • .NETFramework 4.5.2: 4.5.2.0
  • .NETStandard 2.0: 2.0.0.0

Owners

Jaxelr

Authors

Jaxel Rojas

Project URL

https://github.com/Jaxelr/Nancy.Metadata.OpenApi

License

MIT

Tags

openapi nancy metadata

Info

714 total downloads
45 downloads for version 0.11.0
Download (46.55 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
0.11.0 46.55 KB Mon, 23 Sep 2019 15:20:18 GMT 45
0.10.0 39.21 KB Sat, 13 Jul 2019 14:12:57 GMT 65
0.9.0 38.66 KB Sat, 25 May 2019 01:00:47 GMT 56
0.8.1 38.79 KB Fri, 10 May 2019 02:30:26 GMT 61
0.8.0 38.58 KB Wed, 01 May 2019 12:02:00 GMT 54
0.7.0 37.8 KB Mon, 07 Jan 2019 12:01:01 GMT 49
0.6.1 26.88 KB Fri, 10 Aug 2018 20:49:11 GMT 62
0.6.0 26.85 KB Wed, 11 Jul 2018 02:16:37 GMT 49
0.5.2 22.09 KB Tue, 03 Jul 2018 10:27:01 GMT 43
0.5.1 22.08 KB Thu, 07 Jun 2018 15:33:36 GMT 54
0.5.0 22.49 KB Mon, 07 May 2018 23:29:14 GMT 56
0.4.1-pre 11.81 KB Mon, 30 Apr 2018 09:58:01 GMT 61
0.4.0-pre 12.12 KB Thu, 22 Mar 2018 11:59:38 GMT 59