dotsa
dotsa

Reputation: 941

How to use NSwag.CodeGeneration.CSharp?

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.

Upvotes: 3

Views: 9074

Answers (3)

AnthonyLambert
AnthonyLambert

Reputation: 8830

A bit more detail on Rico Suters answer.

You can use the NSwag.CodeGeneration.CSharp nuget and put the code in your server itself. It will then read your servers Json file to generate the csharp client code.

This goes in the program.cs file I trigger it by starting the server with a command line argument. I add code to run this in my Servers projects Post-Build Event.

await GenerateCSharpClient("https://petstore.swagger.io/v2/swagger.json", "PetStoreClient");

Add this as a class to that project.

static async Task GenerateCSharpClient(string openApiLocation, string className)
{
    var document = await OpenApiDocument.FromUrlAsync(openApiLocation);
    var settings = new CSharpClientGeneratorSettings
    {
        UseBaseUrl = false,
        ClassName = className,
        GenerateClientInterfaces = true,
        CSharpGeneratorSettings =
        {
            Namespace = "HttpClients",
        },
    };

    var generator = new CSharpClientGenerator(document, settings);
    var generatedCode = generator.GenerateFile();

    var path = $"../../../{settings.CSharpGeneratorSettings.Namespace}/{settings.ClassName}";
    var file = new FileInfo(path);
    file.Directory.Create();
    await File.WriteAllTextAsync(file.FullName, generatedCode);
}

Upvotes: 0

Isaac Pindado
Isaac Pindado

Reputation: 93

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

Upvotes: 2

Rico Suter
Rico Suter

Reputation: 11858

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.

Upvotes: 3

Related Questions