RicoSuter / NSwag

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

Using OpenApiReference in csproj file is broken after upgrade to v14 #4640

Open jonnybee opened 8 months ago

jonnybee commented 8 months ago

I have tested with NSwag.ApiDescription.Client v14.0.0.preview10 and v14.0.0.preview12

In my csproj file:

    <OpenApiReference Include="OpenAPIs\authorization.json" CodeGenerator="NSwagCSharp" Namespace="AcceptanceTests.Authorization.Clients" ClassName="{controller}Client">
        <SourceUri>https://localhost:44317/openapi</SourceUri>
        <Options>/GenerateClientInterfaces:true /GenerateExceptionClasses:false /AdditionalNamespaceUsages:AcceptanceTests.ApplicationBackend.Clients</Options>
    </OpenApiReference>

and after upgrade to v14.0.0.preview12 and .NET8 i get this build error:

NSwag command line tool for .NET Core Net80, toolchain v14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) Visit http://NSwag.org for more information. NSwag bin directory: C:\Users\jonbek.nuget\packages\nswag.msbuild\14.0.0-preview012\tools\Net80 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 62

When using .NET7 and NSwag.ApiDescription.Client v13.x.x this compiles just fine.

jonnybee commented 8 months ago

I was able to find a workaround - after removing /GenerateExceptionClasses:false from Options the command ran fine. But the error message is not very helpful and it worked just fine with v13.x.x and older versions.

mus65 commented 7 months ago

Same here, we initially ran into #4633 after upgrading to v14 and after applying the workaround (https://github.com/RicoSuter/NSwag/issues/4633#issuecomment-1875176162) , we are running into this error. Removing /GenerateExceptionClasses:false is not a viable workaround since it is still needed to fix https://github.com/dotnet/aspnetcore/issues/18204 .

fretje commented 7 months ago

Apparently you can now use the <NSwagGenerateExceptionClasses> msbuild property (and any other of the nswag options like that). When you do that in stead of adding it to the <Options> property, the command runs fine.

Apparently, if a property has already been defined using the "msbuild property syntax", you can not change it anymore using the "/options syntax" (as it results in that "Unrecognised arguments are present" error), but you can still override it using the "msbuild property syntax".

For example:

<OpenApiReference Include="swagger.json">
    <Namespace>namespace</Namespace>
    <ClassName>classname</ClassName>
    <NSwagGenerateExceptionClasses>false</NSwagGenerateExceptionClasses>
</OpenApiReference>

I found this out by trial and error, and browsing through the source code... not sure if this is actually documented somewhere.

rjpowers10 commented 7 months ago

Thanks @fretje, converting from <Options> to <NSwag...> fixed my issues. I tried looking for documentation on this before finally stumbling upon your comment and didn't find anything.

iSatishYadav commented 4 months ago

Apparently you can now use the <NSwagGenerateExceptionClasses> msbuild property (and any other of the nswag options like that). When you do that in stead of adding it to the <Options> property, the command runs fine.

Apparently, if a property has already been defined using the "msbuild property syntax", you can not change it anymore using the "/options syntax" (as it results in that "Unrecognised arguments are present" error), but you can still override it using the "msbuild property syntax".

For example:

<OpenApiReference Include="swagger.json">
    <Namespace>namespace</Namespace>
    <ClassName>classname</ClassName>
    <NSwagGenerateExceptionClasses>false</NSwagGenerateExceptionClasses>
</OpenApiReference>

I found this out by trial and error, and browsing through the source code... not sure if this is actually documented somewhere.

You sir, are a life-saver.

DavidPx commented 2 months ago

It looks like /GenerateExceptionClasses:false is being added twice to the CLI command that does the actual code generation; here's some sanitized failing build output from when I had /GenerateExceptionClasses:false in the Options attribute:

dotnet --roll-forward-on-no-candidate-fx 2 "/root/.nuget/packages/nswag.msbuild/14.0.7/buildTransitive/../tools/Net80//dotnet-nswag.dll" openapi2csclient /className:MyClient /namespace:MyNamespace.MyClient /GenerateExceptionClasses:false /input:"/pathtojson.json" /output:"obj/MyClient.cs" /GenerateExceptionClasses:false /UseBaseUrl:false /GenerateClientInterfaces:true

My Options attribute also included /UseBaseUrl:false /GenerateClientInterfaces:true; note how /GenerateExceptionClasses:false first appears right after the namespace switch.

After moving to the NSwagGenerateExceptionClasses child element style it only appears once, again right after the namespace switch.

It appears to be a problem with this args length check. Note that this is a link to an older version of the NConsole, v3.12-ish. The current version, 3.13 handles this section differently and might work better. However the openapi tooling pulls down v3.12 so we're stuck with it.