40

What tool can I use to generate a .NET Client to consume from a Swagger definition?

For example, I started trying NSwag Studio and I would like to be able to generate the code to look like the Repository classes I am used to create.

Note to the voters wishing to delete the question: the answers and comments of the question are useful for readers, so deletion is not a good idea. As SO doesn’t allow questions seeking recommendations for books, tools, software libraries, the question should stay closed or (even better) consider to migrate to http://softwarerecs.stackexchange.com

Habeeb
  • 1,020
  • 16
  • 35
Antoine Griffard
  • 1,019
  • 1
  • 9
  • 21

2 Answers2

46

You can use the swagger-codegen tool from the swagger project. It produces C# files that use

  • RestClient for the HTTP calls
  • Newtonsoft.Json for json marshalling
  • .NET DataContract for the models.

You can either download the cli app or use the online editor. The petstore example models look like this:

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace IO.Swagger.Model {

    /// <summary>
    /// 
    /// </summary>
    [DataContract]
    public class Order {
      /// <summary>
      /// Gets or Sets Id
      /// </summary>
      [DataMember(Name="id", EmitDefaultValue=false)]
      [JsonProperty(PropertyName = "id")]
      public long? Id { get; set; }

      /// <summary>
      /// Gets or Sets PetId
      /// </summary>
      [DataMember(Name="petId", EmitDefaultValue=false)]
      [JsonProperty(PropertyName = "petId")]
      public long? PetId { get; set; }
 .... snip ....
}
Pang
  • 9,564
  • 146
  • 81
  • 122
stringy05
  • 6,511
  • 32
  • 38
18

You may want to try OpenAPI Generator (https://github.com/OpenAPITools/openapi-generator), which supports both OpenAPI spec v2 and v3. (OpenAPI spec v2 was formerly known as Swagger spec v2)

There are 3 different C# client generators:

  • csharp
  • csharp-netcore
  • csharp-dotnet2

The project also includes 2 C# server generators: csharp-nancyfx, aspnetcore

If you need help, please open an issue in the Github repo.

Disclosure: I'm the top contributor to both OpenAPI Generator and Swagger Codegen.

William Cheng
  • 10,137
  • 5
  • 54
  • 79
  • Hi, the generator works also as the maven generator for java, producing the code directly during the build? Or you have to produce the code and then export to your project? Thank you – osharko Jul 23 '20 at 07:51
  • You can use the openapi-generator maven plugin: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/integration.md – William Cheng Jul 23 '20 at 09:39
  • How can I integrate the maven plugin into .Net Core project? – osharko Jul 23 '20 at 13:03
  • If it's a .net core project, what about using the cake addin instead? https://github.com/OpenAPITools/openapi-generator/blob/master/docs/integration.md#cake-addin – William Cheng Aug 05 '20 at 03:13