12

I am able to integrate the Swagge UI in my web api using Swashbuckle. I also want to explore the swagger codegen feature. Can somebody help in - how I can integrate swagger codegen into my web api project? Or do I need to download any tool? I want to be able it to host the codegen and pass the json/raml form specs to generate client in .net core.

I am not able to find enough docs on above.

EDIT : I want to know how I can host codegen in my WEBAPI.

Thanks!

arpymastro
  • 751
  • 3
  • 16
  • 34
  • Have you looked into the [documentation and support forum](https://swagger.io/forum/)? There might be a 'getting started' guide – Jamie Taylor Jan 31 '18 at 11:04
  • @JamieTaylor I am not able to understand if I need to download the codegen tool or is there any ways that I can integrate it in the code directly. Like we do for swagger ui – arpymastro Jan 31 '18 at 11:16
  • Could you please clarify what you mean by "integrate Swagger Codegen"? 1) Generate client SDKs for your API so you can provide these SDKs to your users. 2) Host codegen in your app similar to what [Swagger Editor](http://editor.swagger.io) does. 3) Something else? – Helen Jan 31 '18 at 13:14
  • @Helen yes I want to host codegen in my app.. I am already hosting Swagger UI but not able to find how I can host codegen – arpymastro Feb 01 '18 at 03:44
  • Check out https://stackoverflow.com/a/33365276/113116 and https://stackoverflow.com/q/39730438/113116 – Helen Feb 01 '18 at 10:44
  • 1
    @Helen - in urls they have mentioned to use https://generator.swagger.io so I would need to send my files to third party sites. Can this generator be hosted in my env? – arpymastro Feb 01 '18 at 11:39

3 Answers3

3

Now, you can use Nswag. There are several code generator utilities - UI, Console, msbuild.

AndrewSilver
  • 976
  • 2
  • 14
  • 25
2

You should install "Swashbuckle.AspNetCore.Swagger" nuget package by right click your project and click manage nuget packages.

Then you should add these codes into your project startup place eg. Program.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    // Register the Swagger generator, defining one or more Swagger documents
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
    });
}

public void Configure(IApplicationBuilder app)
{
    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();

    app.UseMvc();
}
Metehan Senol
  • 651
  • 4
  • 10
  • That's a fantastically succinct way to describe what needs to be done in order to add Swagger. For a slightly longer form version, I'd recommend [one of my blog posts](https://dotnetcore.gaprogman.com/2017/12/07/giving-dwcheckapi-that-swagger/) (where I take a pre-existing app and add swagger to it, , along with including XML documentation) – Jamie Taylor Jan 31 '18 at 10:36
  • 9
    @Metehan this is for Swagger UI. I am able to generate that. I want details about swagger codegen – arpymastro Jan 31 '18 at 10:38
  • I apologize for my misunderstanding but this is a fairly detailed topic. You can look at blog post who @JamieTaylor pointed above. – Metehan Senol Jan 31 '18 at 10:50
  • @MetehanSenol have you looked into the [documentation and support forum](https://swagger.io/forum/)? There might be a 'getting started' guide – Jamie Taylor Jan 31 '18 at 10:52
  • @MetehanSenol Thanks anyways! – arpymastro Jan 31 '18 at 11:42
  • I have a very similar question and this answer clearly doesn't cover to OP's question. It's pretty well documented how to add swagger to a dotnet project, but how can we generate a javascript client from that swagger automatically similar to swagger codegen. – Dewey Vozel Nov 07 '20 at 04:46
1

It seems like you just want to generate C# from the OpenApi specification (your Swagger implementation provides the input) of your API.

To generate code (e.g. C#) from the OpenApi spec file of your API, you can do something like this:

java -jar .\openapi-generator-cli-5.0.0-beta3.jar generate -i https://localhost:xxxx/api/v1/swagger.json -g csharp

You have to download the OpenApi Generator Jar. Alternatively you can upload your code to a web generator. But I would always run this locally; you never know where your code ends up.

  • 2
    right language, wrong version. He is asking about .NET core (1, 2,or 3) or .NET (5 or 6). Currently, the only backend .NET generators for swaggergen are `csharp` and `csharp-dotnet2`. `csharp` is for .NET Framework 4.5, and `csharp-dotnet2` is for .NET Framework 2.0. – Jacrys May 07 '21 at 17:25