How to generate C# client from Swagger 1.2 spec?

2019-04-22 17:42发布

问题:

There seems to be millions of options out there for every platform, but I'm struggling to find a simple solution for C#. All the ones I have found seem to have given me trouble: either they simply don't work (e.g. http://swaggercodegen.azurewebsites.net/), or only support 2.0 (e.g. AutoRest and NSwag). Half the tools are not even clear what versions they support :-(

I'm aware of the official tool, but that requires JDK 7 which is not currently an option for me.

In desperation I have even tried converting the swagger spec to 2.0, but half the conversion tools I tried didn't work, gave conflicting advice, or I couldn't figure out how to use (I found myself knee deep in nodejs very quickly...is this really the brave new world?! Bring back WSDL ;-) ).

回答1:

You can use the online swagger codegen to create clients without installing anything:

https://generator.swagger.io/

You would choose POST /gen/clients/{language} and enter a body like this:

{
  "options": {},
  "swaggerUrl": "http://petstore.swagger.io/v2/swagger.json"
}

Put your own URL to access the specification. If you need to pass any options, you can put them in the options hash. The list of available options for C# is also available from the generator:

https://generator.swagger.io/api/gen/clients/csharp-dotnet2

{
  "packageName": {
    "opt": "packageName",
    "description": "C# package name (convention: Camel.Case).",
    "type": "string",
    "default": "IO.Swagger"
  },
  "packageVersion": {
    "opt": "packageVersion",
    "description": "C# package version.",
    "type": "string",
    "default": "1.0.0"
  },
  "clientPackage": {
    "opt": "clientPackage",
    "description": "C# client package name (convention: Camel.Case).",
    "type": "string",
    "default": "IO.Swagger.Client"
  }
}