-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Unrecognised arguments are present" after updating from 14.0.8 to 14.1.0 #4938
Comments
Hi @mus65, thanks for reporting this. Some background on what's happening here: When a project has multiple OpenAPI references without any settings, each client generates its own The error you're experiencing is because I made Unfortunately, the MSBuild properties can't reliably know which command-line switches are directly set in I'm very happy with the introduction of MSBuild properties, because it's a huge enabler. Not only does it make the project file easier to read (no more long Personally, I'm not a fan of the magic introduced for the getting-started experience. But it was already there, so I just made it behave consistently. I would recommend everyone to favor the MSBuild properties over setting I'm not sure what's the best approach here. Theoretically, it should be possible to introduce an internal @RicoSuter any thoughts? |
The way we are (still) working around this is by having a copy of the ApiException class in our own namespace and setting the following in the csproj. Obviously having our own copy of the class is not great, but this workaround was good enough for us.
I don't quite understand how this is supposed to work. I just tried this by removing the workaround. We have every client in a separate namespace, so now the ApiException is generated in namespace of Client1, but all other clients fail to compile because Why doesn't NSwag always generate the ApiException class in its own separate namespace (NSwag.*) and all generated clients use that one? This would work regardless of the namespaces of the client. (I assume there is a reason this doesn't work either, I'm just asking. 😄 ). |
There's no need for a workaround anymore. Below are some scenarios, hope that helps. <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="8.0.7" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NSwag.ApiDescription.Client" Version="14.1.0" PrivateAssets="all" />
</ItemGroup>
<!-- Example 1: Generate ApiException in the first file (using a namespace is optional). -->
<!--
<ItemGroup>
<OpenApiReference Include="..\openapi1.json" CodeGenerator="NSwagCSharp" Link="OpenAPIs\openapi1.json" ClassName="ApiClient1" Namespace="ExampleNamespace" />
<OpenApiReference Include="..\openapi2.json" CodeGenerator="NSwagCSharp" Link="OpenAPIs\openapi2.json" ClassName="ApiClient2" Namespace="ExampleNamespace" />
</ItemGroup>
-->
<!-- Example 2: Use your own shared ApiException class (ApiException is never generated). -->
<!--
<ItemGroup>
<OpenApiReference Include="..\openapi1.json" CodeGenerator="NSwagCSharp" Link="OpenAPIs\openapi1.json" ClassName="ApiClient1" Namespace="ExampleNamespace">
<NSwagGenerateExceptionClasses>false</NSwagGenerateExceptionClasses>
<NSwagAdditionalNamespaceUsages>NSwagMultiClientDemo.Shared</NSwagAdditionalNamespaceUsages>
</OpenApiReference>
<OpenApiReference Include="..\openapi2.json" CodeGenerator="NSwagCSharp" Link="OpenAPIs\openapi2.json" ClassName="ApiClient2" Namespace="ExampleNamespace">
<NSwagGenerateExceptionClasses>false</NSwagGenerateExceptionClasses>
<NSwagAdditionalNamespaceUsages>NSwagMultiClientDemo.Shared</NSwagAdditionalNamespaceUsages>
</OpenApiReference>
</ItemGroup>
-->
<!-- Example 3: Every client gets its own generated ApiException class (in different namespaces). -->
<!--
<ItemGroup>
<OpenApiReference Include="..\openapi1.json" CodeGenerator="NSwagCSharp" Link="OpenAPIs\openapi1.json" ClassName="ApiClient1" Namespace="ExampleNamespace1">
<NSwagGenerateExceptionClasses>true</NSwagGenerateExceptionClasses>
</OpenApiReference>
<OpenApiReference Include="..\openapi2.json" CodeGenerator="NSwagCSharp" Link="OpenAPIs\openapi2.json" ClassName="ApiClient2" Namespace="ExampleNamespace2">
<NSwagGenerateExceptionClasses>true</NSwagGenerateExceptionClasses>
</OpenApiReference>
</ItemGroup>
-->
</Project> So depending on your needs, choose one of the following:
Caution There's one gotcha to be aware of though: if you change your project file, the client generator doesn't rerun. Delete the Below I've attached a fully runnable sample to try these out, which I created using the Add Service Reference dialog in Visual Studio.
This is how NSwag worked before I started looking into this. I can only guess about the motivations. What matters to me most is that it's possible to configure it as needed. |
Thanks, Example 3 works well for our case. Setting I still find the default "only generate for the first reference" behaviour a little surprising, but I realize this is a non-trivial problem and this is still a much simpler workaround than before. |
thanks for this update. It works, as Example 3 suggests also thanks for this global property settings suggestion What happens, when you have mixed namespaces in references, some references sharing the same, some having mixed namespaces. You then have to individually manage this option for each one. Shared namespaces must use the Example 1 or Example 2. Based on what you prefer. |
Regression #4891 @bkoelman .
If
/generateResponseClasses:false
is already specified for the API on theOptions
attribute of<OpenApiReference>
in the csproj, this change appears to cause it to be appended again.We simply removed
/generateResponseClasses:false
from the<OpenApiReference>
as a workaround.The text was updated successfully, but these errors were encountered: