Skip to content

christianhelle/apiclientcodegen

Repository files navigation

Build status CLI Tool VSIX VS Mac Smoke Tests

Maintainability Rating Reliability Rating Security Rating Bugs Vulnerabilities Code Smells Coverage

Version Downloads Rating Downloads Rating NuGet

buymeacoffee Join the chat at https://gitter.im/apiclientcodegen/community

REST API Client Code Generator

A collection of Visual Studio C# custom tool code generators for Swagger / OpenAPI specification files

Download

Features

  • Supports Visual Studio 2017, 2019, 2022, andVisual Studio for Mac
  • Add New REST API Client to a project from an OpenAPI specification URL (e.ghttps://petstore.swagger.io/v2/swagger.json) using the following code generators:
  • Define custom namespace for the generated file
  • Auto-updating of generated code file when changes are made to the OpenAPI specification JSON or YAML file
  • Generate code using a configuration files using the following:
    • .nswagconfiguration files fromNSwagStudioby including it in the project and using theGenerate NSwag Studio outputcontext menu
    • .refittersettings files fromRefitterby including it in the project and using theGenerate Refitter outputcontext menu
    • kiota-lock.jsonconfiguration files fromMicrosoft Kiotaby including it in the project and using theGenerate Kiota outputcontext menu

Custom Tools

Custom tools let you associate a tool with an item in a project and run that tool whenever the file is saved

  • NSwagCodeGenerator- Generates a single file C# REST API Client using theNSwag.CodeGeneration.CSharpnuget packagev14.1.0

  • OpenApiCodeGenerator- Generates a single file C# REST API Client usingOpenAPI Generator v7.9.0. The output file is the result of merging all the files generated using the OpenAPI Generator tool with: generate -g csharp --input-spec [swagger file] --output [output file] -DapiTests=false -DmodelTests=false -DpackageName=[default namespace] --skip-overwrite.It is possible to configure the OpenAPI Generator to generate multiple files which will be placed at the same path as the OpenAPI specifications document that was used to generate code, this is done under Tools -> REST API Client Code Generator -> OpenAPI Generator and settingGenerate Multiple Filestotrue

  • KiotaCodeGenerator- Generates a single file C# REST API Client using the Microsoft projectKiota v1.19.1generator. The output file is the result of merging all the files generated using the Kiota dotnet tool with:generate -l CSharp -d [swagger file] -o [output file] -n [default namespace].It is possible to configure the OpenAPI Generator to generate multiple files which will be placed at the same path as the OpenAPI specifications document that was used to generate code, this is done under Tools -> REST API Client Code Generator -> Kiota and settingGenerate Multiple Filestotrue

  • SwaggerCodeGenerator- Generates a single file C# REST API Client usingSwagger Codegen CLI v3.0.34. The output file is the result of merging all the files generated using the Swagger Codegen CLI tool with: generate -l csharp --input-spec [swagger file] --output [output file] -DapiTests=false -DmodelTests=false -DpackageName=[default namespace] --skip-overwrite

  • AutoRestCodeGenerator- Generates a single file C# REST API Client usingAutoRest v3.0.0-beta.20210504.2for OpenAPI v3 andAutoRest v2.0.4417for OpenAPI v2. The resulting file is the equivalent of using the AutoRest CLI tool with: --csharp --input-file=[swagger file] --output-file=[output file] --namespace=[default namespace] --add-credentials

  • RefitterCodeGenerator- Generates a single file C# REST API Client inteface forRefitusingRefitter.Corenuget packagev1.4.0. The output file contains a Refit interface generated byRefitterand contracts generated usingNSwag.CodeGeneration.CSharpnuget package

Dependencies

The custom tool code generators piggy back on top of well known Open API client code generators likeAutoRest,NSwag,OpenAPI Generator,Microsoft Kiota,Refitter,andSwagger Codegen CLI.These tools requireNPMand theJava Runtime Environmentto be installed on the developers machine. Alternative Java SDK implementations such as theOpenJDKworks fine with this extension. By default, the path tojava.exeis read from theJAVA_HOMEenvironment variable, but is also configurable in the Settings screen. Future versions of the extension will include theOpenJDKin the VSIX package

TheSwagger Codegen CLIandOpenAPI Generatorare distributed as JAR files and are downloaded on demand but requires the Java SDK to be installed on the machine.AutoRestis installed on-demand viaNPMas a global tool and uses the latest available version.Microsoft Kiotais installed on-demand as a.NET Tool and requires.NET 7.0. This means that using these custom tools have an initial delay upon first time use.

NSwagStudiois stand alone UI tool for editing a.nswagspecification file for generating code. This tool is optional to install and official installation instructions are available on theNSwag Wiki on Github.IfNSwagStudiois not installed on the machine then the Visual Studio Extension will install theNSwag CLIviaNPMas a global tool using the latest available version.

TheNSwagcode generator produces code that depends on theNewtonsoft.JsonNuGet package

TheRefittercode generator produces code that depends on theRefitNuGet package

TheOpenAPI Generatorcode generator produces code that depends on the following NuGet packages:

The projectKiotacode generator produces code that depends on the following NuGet packages

TheSwagger Codegen CLIcode generator produces code that depends on theRestSharpandJsonSubTypesNuGet packages

TheAutoRestcode generator produces code that depends on theMicrosoft.Rest.ClientRuntimeandNewtonsoft.JsonNuGet packages

This Visual Studio Extension will automatically add the required NuGet packages that the generated code depends on

Screenshots

Add - API Client from OpenAPI Specification

Enter URL to OpenAPI Specification

Solution Explorer Context Menus

NSwag Studio Context Menu

Refitter Context Menu

Kiota Context Menu

Settings

This extension will by default make some assumptions on the installation paths forJava,NSwagandNPMbut also provides option pages for configuring this. TheSwagger Codegen CLIand theOpenAPI GeneratorJAR files are by default downloaded to the user TEMP folder but it is also possible to specify to use existing JAR files

Options - General

Supports customising how AutoRest generates code based on the C# generator settings that the AutoRest CLI tool provides

Options - AutoRest

Supports customising how NSwag generates code using the properties exposed by the NSwag NuGet package

Options - NSwag

Supports customising how the.nswagfile is generated using a subset of the options available in NSwag Studio

Options - NSwag Studio

Supports customising how OpenAPI Generator generates code using the additional optional properties that the OpenAPI Generator CLI tool provides

Options - OpenAPI Generator

Supports customising how Refitter generates the Refit interface. This allows you to configure whether to return the type directly or wrap it inside anIApiResponse<T>.You can also disable generating contracts or XML doc style comments

Options - Refitter

Supports allowing Kiota to generate multiple files

Options - Kiota

This extension collects errors and tracks feature usages to a service calledExceptionlessandAzure Application Insights.This is done anonymously using a support key and a generated anonymous identity based on a secure hash of username@host

Options - Analytics

MacOS

Add - API Client from OpenAPI Specification

Enter URL to OpenAPI Specification

Solution Explorer Context Menus

NSwag Studio Context Menu

Installation

The Visual Studio extension can be installed directly from Visual Studio 2019 and 2022 via theExtensions Dialog Box.The process is best described in the official Microsoft documentation forManaging extensions for Visual Studio

Visual Studio for Mac

This installation process forVisual Studio for Macis currently a bit troublesome as the MonoDevelop Addin Repository is currently not accepting new users so I can't really register and setup my extension.

There are 2 ways of installing my extension on Visual Studio for Mac: Adding a custom extension repository or Installing the.mpackfile directly from the Extensions Manager

Adding a new extension repository

Here's what you need to do:

  • Open the Visual Studio for MacExtension Manager
  • You can do this from the menuVisual Studio->Extentions

Open Extensions Dialog Box

  • Select theGalleryTab
  • Expand the Repository drop down box and selectManage Repositories

Manage Repositories

  • Press on theAddbutton to add a new custom extension repository

Manage Repositories

Add Repository

  • Now my extension repository is added to the list
  • Make sure that this is enabled (indicated by a check box)

Add Repository Dialog

  • You should now be able to see my extensions from theGallerytab

Added Repository

  • By adding my extension repository you will be able to conveniently update my extension using the Visual Studio for Mac Extension Manager

Add Repository

Add Repository

Add Repository

Installing the.mpackfile directly

Here's what you need to do:

  • Download the latest.mpack filefrom theLatest Github Release
  • Now from within Visual Studio for Mac you need to launch theExtensions Dialog Box.You can do this from the menuVisual Studio->Extentions

Open Extensions Dialog Box

  • Click on theInstall from Filebutton

Manually install .mpack file

  • Browse to the.mpack file and select it. You will be prompted with a confirmation dialog

Confirm .mpack file install

  • ClickInstalland restart Visual Studio for Mac

  • To Verify that the Add-in was installed you can re-open the Extensions Dialog Box, select theInstalledtab and expand theIDE Extensions.You should be able to see theREST API Client Code Generator.

  • Uninstalling the Add-in is done in this same dialog box by clicking theUninstallbutton

Manually uninstall Add-in

Cross Platform Command Line Tool

All custom tools mentioned above are also implemented in a cross platform command line application

Requirements

  • .NET 6.0 runtime
  • Java Runtime Environment
  • NPM

Installation

The tool can be installed as a.NET Core global tool that you can call from the shell / command line

dotnet tool install --global rapicgen

or by following the instructionshereto install a specific version of tool

Usage

Since the tool is published as a.NET Core Tool, it can be launched from anywhere using any command line interface by callingrapicgen. The help information is displayed when not specifying any arguments torapicgen

Usage: rapicgen [command] [options]

Options:
-v|--verbose Show verbose output
-?|-h|--help Show help information.

Commands:
csharp Generate C# API clients
jmeter Generate Apache JMeter test plans
openapi-generator Generate code using OpenAPI Generator (v7.9.0).
See supported generators at https://openapi-generator.tech/docs/generators/
typescript Generate TypeScript API clients

Run 'rapicgen [command] -?|-h|--help' for more information about a command.

Some help information is also provided per command and can be launched by

rapicgen [command name] -?

or

rapicgen [command name] [sub command name] -?

For example:

rapicgen csharp -?

will output this:

Generate C# API clients

Usage: rapicgen csharp [command] [options]

Options:
-?|-h|--help Show help information.

Commands:
autorest AutoRest (v3.0.0-beta.20210504.2)
kiota Microsoft Kiota (v1.19.1)
nswag NSwag (v14.1.0)
openapi OpenAPI Generator (v7.9.0)
refitter Refitter (v1.4.0)
swagger Swagger Codegen CLI (v3.0.34)

Run 'csharp [command] -?|-h|--help' for more information about a command.

and

rapicgen csharp autorest -?

will output this:

Generate Swagger / Open API client using AutoRest

Usage: run autorest [options] <swaggerFile> <namespace> <outputFile>

Arguments:
swaggerFile Path to the Swagger / Open API specification file
namespace Default namespace to in the generated code
outputFile Output filename to write the generated code to. Default is the swaggerFile.cs

Options:
-?|-h|--help Show help information

or

rapicgen typescript -?

will output this:

Generate TypeScript API client

Usage: rapicgen typescript [options] <generator> <swaggerFile> <outputPath>

Arguments:
generator The tech stack to use for the generated client library
Allowed values are: Angular, Aurelia, Axios, Fetch, Inversify, JQuery, NestJS, Node, ReduxQuery,
Rxjs.
Default value is: Angular.
swaggerFile Path to the Swagger / Open API specification file
outputPath Output folder to write the generated code to
Default value is: typescript-generated-code.

Options:
-nl|--no-logging Disables Analytics and Error Reporting
-?|-h|--help Show help information.

Usage Examples:

Let's say we have a OpenAPI Specifications document calledSwagger.json

For starters, we can use the Swagger Petstore spec. Here's an example powershell script for downloading it

Invoke-WebRequest -Uri https://petstore.swagger.io/v3/swagger.json -OutFile Swagger.json

In case you don't have the CLI tool installed you can install it by

dotnet tool install --global rapicgen

Here's an example of how to generate code usingAutoRest

rapicgen csharp autorest Swagger.json GeneratedCode./AutoRestOutput.cs

Here's an example of how to generate code usingKiota

rapicgen csharp kiota Swagger.json GeneratedCode./OpenApiOutput.cs

Here's an example of how to generate code usingNSwag

rapicgen csharp nswag Swagger.json GeneratedCode./NSwagOutput.cs

Here's an example of how to generate code usingSwagger Codegen CLI

rapicgen csharp swagger Swagger.json GeneratedCode./SwaggerOutput.cs

Here's an example of how to generate code usingOpenAPI Generator

rapicgen csharp openapi Swagger.json GeneratedCode./OpenApiOutput.cs

Here's an example of how to generate codeJMetertest plans

rapicgen jmeter Swagger.json

Here's an example of how to generate code forTypeScript

rapicgen typescript Angular Swagger.json

For tips and tricks on software development, check outmy blog

If you find this useful and feel a bit generous then feel free tobuy me a coffee ☕