![Donate](https://img.shields.io/badge/donate-via PayPal-green.svg?style=flat)
NSwag is a Swagger 2.0 API (OpenAPI) toolchain for .NET, Web API, TypeScript (jQuery, AngularJS, Angular 2, Aurelia, KnockoutJS, and more) and other platforms, written in C#. The Swagger specification uses JSON and JSON Schema to describe a RESTful web API. The NSwag project provides tools to generate Swagger specifications from existing ASP.NET Web API controllers and client code from these Swagger specifications.
The project combines the functionality of Swashbuckle (Swagger generation) and AutoRest (client generation) in one tool chain. This way a lot of incompatibilites can be avoided and features which are not well described by the Swagger specification or JSON Schema are better supported (e.g. inheritance, enum and reference handling). The NSwag project heavily uses NJsonSchema for .NET for JSON Schema handling and C#/TypeScript class/interface generation.
The project is developed and maintained by Rico Suter and other contributors.
Ways to use the toolchain:
- Simple to use Windows GUI, NSwagStudio
- Via command line (Windows, Mac and Linux support through Mono or .NET Core console binary)
- By using the Swagger or Swagger UI OWIN and ASP.NET Core Middlewares
- In your C# code, via NuGet
- In your MSBuild targets
- Generate code with T4 templates in Visual Studio
- In your Cake scripts using Cake.NSwag (external community project, may not use latest NSwag version)
Tutorials
- Integrate the NSwag toolchain into your ASP.NET Web API project
- Generate an Angular 2 TypeScript client from an existing ASP.NET Web API web assembly
Swagger Generators:
- ASP.NET Web API assembly to Swagger (supports .NET Core)
- WebApiToSwaggerGenerator
- Generates a Swagger specification for Web API controllers
- WebApiAssemblyToSwaggerGenerator
- Generates a Swagger specification for controllers in an external Web API assembly
- Also supports loading of .NET Core assemblies
- AssemblyTypeToSwaggerGenerator
- Generates a Swagger specification containing only types from .NET assemblies
- WebApiToSwaggerGenerator
Code Generators:
- TypeScript Client
- SwaggerToTypeScriptClientGenerator
- Generates TypeScript clients from a Swagger specification
- Available templates/supported libraries:
- JQuery with Callbacks,
JQueryCallbacks
- JQuery with promises
JQueryPromises
- AngularJS using $http,
AngularJS
- Angular 2 using the http service,
Angular2
- window.fetch API and ES6 promises,
Fetch
(use this template in your React/Redux app) - Aurelia using the HttpClient from aurelia-fetch-client,
Aurelia
(based on the Fetch template)
- JQuery with Callbacks,
- SwaggerToTypeScriptClientGenerator
- CSharp Client
- SwaggerToCSharpClientGenerator
- Generates C# clients from a Swagger specification
- Generates POCOs or classes implementing INotifyPropertyChanged supporting DTOs
- SwaggerToCSharpClientGenerator
- CSharp WebAPI Controllers (contract first/schema first development)
- SwaggerToCSharpControllerGenerator
- Generates Web API Controllers based on a Swagger specification
- SwaggerToCSharpControllerGenerator
Downloads
- Download latest NSwagStudio MSI installer (Windows Desktop application)
- Download latest NSwag command line tools and NSwagStudio as ZIP archive
- Download latest Build Artifacts from AppVeyor (command line tools and NSwagStudio binaries)
NuGet Packages
- NSwag.Core (PCL 259): The Swagger reader and writer classes (Source Code)
- NSwag.Annotations (PCL 259): Attributes to decorate Web API controllers to control the Swagger generation (Source Code)
- NSwag.CodeGeneration (PCL 259): Classes to generate Swagger specifications from Web API controllers and C# and TypeScript clients (Source Code)
- NSwag.AssemblyLoader (.NET 4.5+): Classes to load assemblies in an isolated AppDomain and generate Swagger specs from Web API controllers
- NSwag.AssemblyLoaderCore (.NET Core, .NETStandard 1.6): Classes to load assemblies in an AssemblyLoaderContext and generate Swagger specs from Web API controllers
- NSwag.MSBuild (MSBuild .targets): Adds a .targets file to your Visual Studio project, so that you can run the NSwag command line tool in an MSBuild target
- NSwag.AspNetCore (.NETStandard 1.6 and .NET 4.5.1+):
- NSwag.AspNet.Owin (.NET 4.5+): OWIN Middlewares for serving Swagger specifications and Swagger UI
- NSwag.AspNet.WebApi (.NET 4.5+): ASP.NET Web API filter which serializes exceptions (JsonExceptionFilterAttribute)
- NSwagStudio (Chocolatey, Windows): Package to install the NSwagStudio and command line tools via Chocolatey
- NSwag.Commands (PCL 259): Commands for the command line tool implementations and UI
- NSwag.ConsoleCore (PCL 259): Command line tool for .NET Core (
dotnet nswag
)
The NuGet packages may require the Microsoft.NETCore.Portable.Compatibility package on .NET Core/UWP targets (if mscorlib is missing).
The following code shows how to read a Swagger specification and generate C# client classes to call the described web services:
var swaggerSettings = new WebApiToSwaggerGeneratorSettings();
var swaggerGenerator = new WebApiToSwaggerGenerator(swaggerSettings);
var document = swaggerGenerator.GenerateForController<PersonsController>();
var clientSettings = new SwaggerToCSharpClientGeneratorSettings
{
ClassName = "MyClass",
Namespace = "MyNamespace"
};
var clientGenerator = new SwaggerToCSharpClientGenerator(document, clientSettings);
var code = clientGenerator.GenerateFile();
Check out the project Wiki for more information.
The generators can be used in a comfortable and simple Windows GUI called NSwagStudio: