Nswag: Multiple controllers and explicit ClassName generates duplicate GeneratedCode attributes in client

Created on 5 Oct 2016  路  6Comments  路  Source: RicoSuter/NSwag

When you have an assembly containing two controllers, and explicitly specify the ClassName when generating the client, the GeneratedCode attribute is specified once per each partial class declaration.

Example code for generating client:

var swaggerSettings = new WebApiToSwaggerGeneratorSettings();
var swaggerGenerator = new WebApiToSwaggerGenerator(swaggerSettings);
var service = swaggerGenerator.GenerateForControllers(new[] { typeof(EchoController), typeof(GreeterController) });
var clientSettings = new SwaggerToCSharpClientGeneratorSettings
{
    ClassName = "MyClass",
};
var clientGenerator = new SwaggerToCSharpClientGenerator(service, clientSettings);
var code = clientGenerator.GenerateFile().Dump();

Resulting generated client code (redacted):

namespace MyNamespace
{
    [GeneratedCode("NSwag", "6.3.6121.29191")]
    public partial class MyClass 
    {
        // ...
    }

    [GeneratedCode("NSwag", "6.3.6121.29191")]
    public partial class MyClass 
    {
        // ...
    }
}

This will not compile, because the GeneratedCode attribute specifies AllowMultiple=false

question

All 6 comments

If you have multiple controllers, you have to either choose OperationGenerationMode.SingleClientFromOperationId or use the placeholder {controller} in your controller class name.

So merging multiple controllers into a single client is not supported? That would be good to have documented :)

Also, some type of warning would be nice if the configuration provided will generate invalid code.

It is supported with the OperationGenerationMode.SingleClientFromOperationId setting...

Got it, thanks. Still, would be nice with a warning :)

Got it, thanks. Still, would be nice with a warning :)

You are right...

Here's the documentation:
https://github.com/NSwag/NSwag/wiki/ClientGeneratorBaseSettings

Was this page helpful?
0 / 5 - 0 ratings