April 27, 2020
Not everyone knows that swagger gives you more than just a nice UI to expose your APIs. It actually exposes a JSON endpoint that defines your API. Tools like swagger codegen can use that endpoint to create a complete consuming project in an array of programming languages.
Its a bit of a pain to install and so the easiest way to do it is to use the swaggerapi/swagger-codegen-cli Docker image.
For example if you want to create a project to consume the petstore swagger api - https://petstore.swagger.io/
You can run:
docker run -v %CD%:/local swaggerapi/swagger-codegen-cli generate -l csharp -o /output/csharp -i https://petstore.swagger.io/v2/swagger.json
This will pull and then run the latest version of the Docker image. It will use a volume mapping from the current directory to the local folder in the image. The entry point will be the generate executable. Everything past the generate command is parameters for the generate executable.
What is nice about doing this is that you don\'t have to only use C#, it supports quite a few languages and its as easy as changing the -l parameter to create a different one.
One thing to note: When I first tried this to consume a swagger api at my work it kept on failing because of untrusted certificates. To fix that, I add the following two parameters to the end.