RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.72k stars 1.29k forks source link

v14 regression, caused by NConsole update? #4843

Open AlexSikilinda opened 6 months ago

AlexSikilinda commented 6 months ago

This one may be an edge case. I don't think it's a bug in NSwag yet I think it's the best place to discuss it.

Background

We have a .NET 8 project referencing two swagger files via the Visual Studio "Connected Services/Service References" feature, so the relevant part of .csproj looks like this:

<ItemGroup>
    <OpenApiReference Include="OpenAPIs\file1.json" CodeGenerator="NSwagCSharp" Namespace="Namespace1" ClassName="FirstClientService">
        <SourceUri>http://validOpenApiUrl1</SourceUri>
        <Options>/AdditionalNamespaceUsages:ApiExceptionNamespace /GenerateExceptionClasses:false</Options>
    </OpenApiReference>
    <OpenApiReference Include="OpenAPIs\file2.json" CodeGenerator="NSwagCSharp" Namespace="Namespace2" ClassName="SecondClientService">
        <SourceUri>http://validOpenApiUrl2</SourceUri>
        <Options>/AdditionalNamespaceUsages:ApiExceptionNamespace /GenerateExceptionClasses:false</Options>
    </OpenApiReference>
</ItemGroup>

We use the same exception class added to ApiExceptionNamespace manually, and we instruct NSwag to not generate ApiException via /GenerateExceptionClasses:false added to each swagger file (see above).

Problem

When we dotnet build the project, for some reason, for the second client, /GenerateExceptionClasses:false is inserted twice:

GenerateNSwagCSharp:
    dotnet --roll-forward-on-no-candidate-fx 2 "/root/.nuget/packages/nswag.msbuild/14.0.7/buildTransitive/../tools/Net80//dotnet-nswag.dll" openapi2csclient /className:SecondClientService /namespace:Namespace2 /GenerateExceptionClasses:false /input:"validInput" /output:"obj/client.cs" /AdditionalNamespaceUsages:ApiExceptionNamespace /GenerateExceptionClasses:false

It used to work fine with v13, but updating to v14 causes the second file generation to fail with:

NConsole.UnusedArgumentException: Unrecognised arguments are present: []
     at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
     at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
     at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor.cs:line 65

This is caused by /GenerateExceptionClasses:false being inserted twice for the second file's command during dotnet build, and if we remove it from the second file's <Options> in .csproj - it works (/GenerateExceptionClasses:false is still inserted automatically, but now we have it only once).

I believe it is caused by updating NConsole version in v14, but I can be wrong.

The root cause, probably, is this magical automatic addition of /GenerateExceptionClasses:false during dotnet build, but I validated this behavior was the same in .NET 7 and probably earlier, so decided to create the issue here.

Thanks!

CraftyGeorge commented 4 months ago

We also encountered the same issue described above, and can confirm the workaround as described works (many thanks!)