Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSwag.CodeGeneration.CSharp?

Tags:

nswag

I am trying to generate .NET Core Web API Controllers from a .yaml file.

There is an example of running this command:

dotnet "/.../dotnet-nswag.dll" openapi2cscontroller /input:https://somewhere.com/swagger.yaml /classname:MyResource /namespace:Com.Example.MyResource /output:Controllers/ResourceController.cs /UseLiquidTemplates:true /AspNetNamespace:"Microsoft.AspNetCore.Mvc" /ControllerBaseClass:"Microsoft.AspNetCore.Mvc.Controller"

How is this command relates to NSwag.CodeGeneration.CSharp ?

Whats is this parameter? "/.../dotnet-nswag.dll" Is this coming from npm install of the tools?

How can i generate ApiControllers on my Spec project build in Visual Studio?

Thank you.

like image 692
dotsa Avatar asked Jun 21 '26 08:06

dotsa


2 Answers

The actual implementation of the generator is in the NSwag.CodeGeneration.CSharp nuget package.

You can use this directly in you code to programmatically read a spec and generate code to a string. dotnet-nswag.dll is a command line tool with commands which translate to a call to a generator in this package.

like image 99
Rico Suter Avatar answered Jun 24 '26 21:06

Rico Suter


For commandline usage with dotnet cli, simpliest options is just execute run command passing as argument the nswag configuration file with the required settings for the source code generation.

dotnet C:\Users\demo\.nuget\packages\nswag.msbuild\13.4.2\tools\NetCore31\dotnet-nswag.dll run config.nswag
NSwag command line tool for .NET Core NetCore31, toolchain v13.4.2.0 (NJsonSchema v10.1.11.0 (Newtonsoft.Json v12.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: C:\Users\demo\.nuget\packages\nswag.msbuild\13.4.2\tools\NetCore31

Executing file 'config.nswag' with variables ''...
Done.

Duration: 00:00:01.8555952

Or you can use additional commands like the one you posted (openapi2cscontroller) detailed in the wiki https://github.com/RicoSuter/NSwag/wiki/CommandLine

like image 38
Isaac Pindado Avatar answered Jun 24 '26 21:06

Isaac Pindado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!