All Projects → zijianhuang → openapiclientgen

zijianhuang / openapiclientgen

Licence: MIT License
Generate C# and TypeScript client codes from Open API / Swagger definitions

Programming Languages

typescript
32286 projects
C#
18002 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to openapiclientgen

Webapiclientgen
Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
Stars: ✭ 134 (+332.26%)
Mutual labels:  aurelia, web-api, axios, fetch-api
Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+3248.39%)
Mutual labels:  axios, fetch-api
Php Fetch
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP
Stars: ✭ 95 (+206.45%)
Mutual labels:  web-api, fetch-api
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (+174.19%)
Mutual labels:  axios, fetch-api
Nswag
The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
Stars: ✭ 4,825 (+15464.52%)
Mutual labels:  aurelia, nswag
odin
Data-structure definition/validation/traversal, mapping and serialisation toolkit for Python
Stars: ✭ 24 (-22.58%)
Mutual labels:  yaml
fetchingInReact
💎📷 Fetching data from Unsplash.com in React
Stars: ✭ 23 (-25.81%)
Mutual labels:  axios
Covid19-Stats-IN
This app helps in tracking COVID-19 cases in India using covid19India apis
Stars: ✭ 13 (-58.06%)
Mutual labels:  axios
tests-ionic2-and-aurelia-framework7
Test TODO applications to compare ionic2 and Aurelia + Framework7
Stars: ✭ 14 (-54.84%)
Mutual labels:  aurelia
python-yamlordereddictloader
(DEPRECATED) YAML loader and dumper for PyYAML allowing to keep keys order.
Stars: ✭ 25 (-19.35%)
Mutual labels:  yaml
Obsidian-Markdown-Parser
This repository will give you tools to parse and fetch useful informations of your notes in your Obsidian vault.
Stars: ✭ 32 (+3.23%)
Mutual labels:  yaml
Code-VueWapDemo
“Vue教程--Wap端项目搭建从0到1”的源码
Stars: ✭ 19 (-38.71%)
Mutual labels:  axios
music
🎵vue 像素级还原mac客户端网易云音乐
Stars: ✭ 34 (+9.68%)
Mutual labels:  axios
dby
Simple Yaml DB
Stars: ✭ 47 (+51.61%)
Mutual labels:  yaml
php-helpers
A Collection of useful php helper functions.
Stars: ✭ 26 (-16.13%)
Mutual labels:  yaml
pyaml env
Parse YAML configuration with environment variables in Python
Stars: ✭ 36 (+16.13%)
Mutual labels:  yaml
StoreCleanArchitecture-NET
This is a basic project to demonstrate an introduction about the implementation of Clean Architecture on .NET
Stars: ✭ 19 (-38.71%)
Mutual labels:  web-api
Config
PHP library for simple configuration management
Stars: ✭ 39 (+25.81%)
Mutual labels:  yaml
crystalizer
(De)serialize any Crystal object - out of the box. Supports JSON, YAML and Byte format.
Stars: ✭ 32 (+3.23%)
Mutual labels:  yaml
yaask
Make your yaml configurable with interactive configurations!
Stars: ✭ 15 (-51.61%)
Mutual labels:  yaml

OpenApiClientGen

Reading an Open API / Swagger YAML/JSON definition file, OpenApiClientGen generates strongly typed client API codes in C# and in TypeScript for Angular, Axios, Fetch API, Aurelia and jQuery.

This program is based on Fonlow.TypeScriptCodeDomCore and Fonlow.Poco2TsCore which are core components of WebApiClientGen, thus the codes generated share similar characteristics.

Hints

If you are using ASP.NET or ASP.NET Core to develop Web API and would support C# clients and TypeScript clients, WebApiClientGen may provide an alternative solution that covers what offered by Swashbuckle+NSwag. And in some contexts it is more efficient, effective and comprehensive to use WebApiClientGen in SDLC without involving Open API / Swagger YAML/JSON definitions.

Installation

OpenApiClientGen is a .NET Core console app.

Prerequisites

  • .NET 6.

Remarks

  • The generated C# codes could be built with .NET Frameworks in addition to .NET.

Source Installation

Check out this repository or one of its tags, then do a release build or a Visual Stuido's Publish.

Hints

The plugin assemblies should be copied accordingly after a release build.

Binary Download

Download the zip files and extract to a local folder.

Code Generation

When running the program without valid parameters, you get some simple hints:

Parameter 1: Open API YAML/JSON definition file
Parameter 2: Settings file in JSON format.
Example:  
  Fonlow.OpenApiClientGen.exe my.yaml
  Fonlow.OpenApiClientGen.exe my.yaml myproj.json
  Fonlow.OpenApiClientGen.exe my.yaml ..\myproj.json

A typical CodeGen JSON file is like this "DemoCodeGen.json":

{
	"ClientNamespace": "My.Pet.Client",
	"ClientLibraryProjectFolderName": "./Tests/DemoClientApi",
	"ContainerClassName": "PetClient",
	"ClientLibraryFileName": "PetAuto.cs",
	"ActionNameStrategy": 4,
	"UseEnsureSuccessStatusCodeEx": true,
	"DecorateDataModelWithDataContract": true,
	"DataContractNamespace": "http://pet.domain/2020/03",
	"DataAnnotationsEnabled": true,
	"DataAnnotationsToComments": true,
	"HandleHttpRequestHeaders": true,

	"Plugins": [
		{
			"AssemblyName": "Fonlow.OpenApiClientGen.NG2",
			"TargetDir": "./ng2/src/clientapi",
			"TSFile": "ClientApiAuto.ts",
			"AsModule": true,
			"ContentType": "application/json;charset=UTF-8"
		}
	]

}

You may run the code generator through a batch file:

cd %~dp0
c:\green\OpenApiClientGen\Fonlow.OpenApiClientGen.exe some_service.yaml mycodegen.json

The JSON file is mapped to the following which describes each setting:

public class Settings
{
	/// <summary>
	/// The generated codes should be contained in a namespace. The default is My.Namespace.
	/// </summary>
	public string ClientNamespace { get; set; } = "My.Namespace";

	/// <summary>
	/// To compose client function name through removing path prefix. Typically / or /api. The default is /.
	/// The lenght of the prefix is used to remove path prefix. Applied when ActionNameStrategy is PathMethodQueryParameters.
	/// </summary>
	public string PathPrefixToRemove { get; set; } = "/";

	public ActionNameStrategy ActionNameStrategy { get; set; }

	/// <summary>
	/// The default is \w* for picking up alphanumeric words from some crikey operationIds through matching a list of words 
	/// which will be merged into a function name in Pascal or camel casing. 
	/// Applied when ActionNameStrategy is NorrmalizedOperationId.
	/// </summary>
	public string RegexForNormalizedOperationId { get; set; } = @"\w*";

	public ContainerNameStrategy ContainerNameStrategy { get; set; }

	/// <summary>
	/// Container class name when ContainerNameStrategy is None. The default is Misc.
	/// </summary>
	public string ContainerClassName { get; set; } = "Misc";

	/// <summary>
	/// Suffix of container class name if ContainerNameStrategy is not None. The default is "Client".
	/// </summary>
	public string ContainerNameSuffix { get; set; } = "Client";

	/// <summary>
	/// Assuming the client API project is the sibling of Web API project. Relative path to the WebApi project should be fine. C# only.
	/// </summary>
	public string ClientLibraryProjectFolderName { get; set; }

	/// <summary>
	/// The name of the CS file to be generated under client library project folder. The default is OpenApiClientAuto.cs.
	/// </summary>
	public string ClientLibraryFileName { get; set; } = "OpenApiClientAuto.cs";

	/// <summary>
	/// Generated data types will be decorated with DataContractAttribute and DataMemberAttribute in C#.
	/// </summary>
	public bool DecorateDataModelWithDataContract { get; set; }

	/// <summary>
	/// When DecorateDataModelWithDataContract is true, this is the namespace of DataContractAttribute. For example, "http://mybusiness.com/09/2019
	/// </summary>
	public string DataContractNamespace { get; set; }

	/// <summary>
	/// Serialize enum to string. For C#, effective if DecorateDataModelWithDataContract is true, and the enum type is decorated by
	/// [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] or [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))].
	/// For TypeScript, the output is string enums.
	/// </summary>
	public bool EnumToString { get; set; }

	/// <summary>
	/// Decorate the Data Model with the System.SerializableAttribute attribute
	/// </summary>
	public bool DecorateDataModelWithSerializable { get; set; }

	/// <summary>
	/// For .NET client, generate both async and sync functions for each Web API function. When false, only async.
	/// </summary>
	public bool GenerateBothAsyncAndSync { get; set; }

	/// <summary>
	/// Replace EnsureSuccessStatusCode with EnsureSuccessStatusCodeEx for specific unsuccessful HTTP status handling, which throws YourClientWebApiRequestException.
	/// </summary>
	public bool UseEnsureSuccessStatusCodeEx { get; set; }

	/// <summary>
	/// Default  is true so the code block is included in the generated codes.
	/// Defined if UseEnsureSuccessStatusCodeEx is true. Respective code block will be included the code gen output. However, if you have a few client APIs generated to be used in the same application,
	/// and you may want these client APIs share the same code block, then put the WebApiRequestException code block to an assembly or a standalone CS file.
	/// </summary>
	public bool IncludeEnsureSuccessStatusCodeExBlock { get; set; } = true;

	/// <summary>
	/// System.ComponentModel.DataAnnotations attributes are to be copied over, including Required, Range, MaxLength, MinLength and StringLength. Applied to C# only.
	/// What defined in "default" of YAML will become the default value of respective property.
	/// </summary>
	public bool DataAnnotationsEnabled { get; set; }

	/// <summary>
	/// System.ComponentModel.DataAnnotations attributes are translated into Doc Comments, 
	/// including Required, Range, MaxLength, MinLength, StringLength, DataType and RegularExpression. Applied to C# and TypeScript.
	/// </summary>
	public bool DataAnnotationsToComments { get; set; }

	/// <summary>
	/// Function parameters contain a callback to handle HTTP request headers
	/// </summary>
	public bool HandleHttpRequestHeaders { get; set; }

	/// <summary>
	/// Allow cancellation in Send
	/// </summary>
	public bool CancellationTokenEnabled { get; set; }

	/// <summary>
	/// Use System.Text.Json instead of Newtonsoft.Json
	/// </summary>
	public bool UseSystemTextJson { get; set; }

	/// <summary>
	/// For date type, generate DateOnly property. Default true. If false, generate DateTimeOffset decorated by System.ComponentModel.DataAnnotations.DataTypeAttribute(System.ComponentModel.DataAnnotationsDataType.Date)
	/// </summary>
	public bool DateToDateOnly { get; set; } = true;

	/// <summary>
	/// Generated data types will be decorated with System.Text.Json.Serialization.JsonPropertyNameAttribute or Newtonsoft.Json.JsonPropertyAttribute with the original property name defined in YAML.
	/// </summary>
	public bool DecorateDataModelWithPropertyName { get; set; }

	/// <summary>
	/// OpenApClinetGen declares all value type properties including enum properties as nullable by default in generated C#, and all properties as nullable by default in generated TypeScript codes, unless the property is required. 
	/// This is to prevent serializer from creating payload for properties not assigned.  
	/// There might be situations in which you don't want such default features and want the codegen to respect OpenApi v3 option nullable. Then turn this setting to true, which affects generated C# codes only.
	/// Please note, Some Open API definition files do not define nullable for some premitive types and enum, however, the respective backends do not expect some properties presented in the payload of the request.
	/// therefore you need to build some integration test suites to find out what the backend would like.
	/// If the YAML file defines a reference type property as nullable, the codegen ignores this setting since in C# a nullable reference type property is invalid, unless you set UseNullableReferenceType to true.
	/// </summary>
	public bool DisableSystemNullableByDefault { get; set; }

	/// <summary>
	/// Use T? instead of System.Nullable<T> for value types, while by default System.Nullable<T> is used. C# 2.0 feature
	/// </summary>
	public bool UseNullableQuestionMark { get; set; }

	/// <summary>
	/// Use T? for reference types. C# 8.0 feature: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types
	/// https://docs.microsoft.com/en-us/dotnet/csharp/nullable-migration-strategies: The global nullable context does not apply for generated code files.
	/// Therefore it is up to you application programmers to make the compiler recognize the generated file is not of generated codes.
	/// Check test case: Test_vimeo()
	/// </summary>
	public bool UseNullableReferenceType { get; set; }

	/// <summary>
	/// Create the Model classes only
	/// </summary>
	public bool GenerateModelsOnly { get; set; }

	/// <summary>
	/// By default, array type will be array in generated C#. You may generated IEnumerable and some of its derived types.
	/// </summary>
	public ArrayAsIEnumerableDerived ArrayAs { get; set; }

	/// <summary>
	/// Use pascal case for properties and model class names
	/// </summary>
	public bool UsePascalCase { get; set; }

	/// <summary>
	/// Prefix class names with enclosing Type name. Default True.
	/// </summary>
	public bool PrefixWithTypeName { get; set; } = true;

	/// <summary>
	/// Create destination folder if not exists. Applied to both CS and TS.
	/// </summary>
	public bool CreateFolder { get; set; }

	/// <summary>
	/// Meta for plugins that generate TypeScript/JavaScript codes.
	/// </summary>
	public JSPlugin[] Plugins { get; set; }
}

public enum ActionNameStrategy
{
	/// <summary>
	/// Either OperationId or MethodQueryParameters
	/// </summary>
	Default = 0,

	OperationId = 1,

	/// <summary>
	/// Compose something like GetSomeWhereById1AndId2. Generally used with ContainerNameStrategy.Path
	/// </summary>
	MethodQueryParameters = 2,

	PathMethodQueryParameters = 3,

	/// <summary>
	/// According to Open API specification, it is RECOMMENDED that the naming of operationId follows common programming naming conventions. 
	/// However, some YAML may name operationId after a valid function name. For example, "list-data-sets", "Search by name" or "SearchByName@WkUld". 
	/// Regular expression (regex) may be needed to pick up alphanumeric words from such operationId and create a valid function name.
	/// The default RegexForNormalizedOperationId is /w*.
	/// </summary>
	NormalizedOperationId = 4,
}

public enum ContainerNameStrategy
{
	/// <summary>
	/// All client functions will be constructed in a god class named after ContainerClassName
	/// </summary>
	None,

	/// <summary>
	/// Use tags
	/// </summary>
	Tags,

	/// <summary>
	/// Use path as resource for grouping, as a container class name.
	/// </summary>
	Path,
}

public enum ArrayAsIEnumerableDerived
{
	Array,
	IEnumerable,
	IList,
	ICollection,
	IReadOnlyList,
	IReadOnlyCollection,

	List,
	Collection,
	ReadOnlyCollection,
}

public class JSPlugin
{
	/// <summary>
	/// Assembly file name without extension dll and dir. The assembly file should be in the same directory of the main program.
	/// </summary>
	public string AssemblyName { get; set; }

	/// <summary>
	/// Related path or absolute path. It is recommended to use slash / to sepparate. If you use backlash \, \\ should be used.
	/// </summary>
	public string TargetDir { get; set; }

	/// <summary>
	/// Generated TS file name. For example, MyNgClientAuto.ts
	/// </summary>
	public string TSFile { get; set; }

	///// <summary>
	///// HTTP content type used in POST of HTTP of NG2. For example, "application/json;charset=UTF-8".
	///// </summary>
	public string ContentType { get; set; }

	/// <summary>
	/// True to have "export namespace"; false to have "namespace". jQuery wants "namespace" while Angular 2+ wants "export namespace".
	/// </summary>
	public bool AsModule { get; set; }
}

For more details, especially the contexts of using these settings, please check Settings Explained.

Examples of Generated Codes

OpenApiClientGen had been tested upon over 1000 Open API definitions in v3 formats and in v2 formats.

Comparison with NSwag

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].