NSwag: 'Missing ApiException' compiler error
Currently I have a project that uses multiple openapi.json v3 files. We used the official openapi tools described in this link: https://docs.microsoft.com/en-us/aspnet/core/web-api/microsoft.dotnet-openapi?view=aspnetcore-3.1
Because of the following code the generated c# client do not compile:
<ItemGroup>
<!-- @(CurrentOpenApiReference) item group will never contain more than one item. -->
<CurrentOpenApiReference>
<Command>$(_NSwagCommand) openapi2csclient /className:%(ClassName) /namespace:%(Namespace)</Command>
</CurrentOpenApiReference>
<CurrentOpenApiReference>
<Command Condition="! %(FirstForGenerator)">%(Command) /GenerateExceptionClasses:false</Command>
</CurrentOpenApiReference>
<CurrentOpenApiReference>
<Command>%(Command) /input:"%(FullPath)" /output:"%(OutputPath)" %(Options)</Command>
</CurrentOpenApiReference>
</ItemGroup>
Why did you make this design decision of not generating the Exception code for the remaining clients? The commented affirmation of only one item is not true.
I managed it to work by adding a AdditionalNamespaceUsages options but for me it feels like a hack.
Below I added a sample of a csproj to reproduce the issue:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="NSwag.ApiDescription.Client" Version="13.5.0" />
</ItemGroup>
<ItemGroup>
<OpenApiReference Include=".\OpenApi1.json">
<Namespace>Integration.ExternalApi1</Namespace>
</OpenApiReference>
<OpenApiReference Include=".\OpenApi2.json">
<Namespace>Integration.ExternalApi2</Namespace>
<!-- Workaround to make the generated code compile -->
<!--<Options>/AdditionalNamespaceUsages:Integration.ExternalApi1</Options>-->
</OpenApiReference>
</ItemGroup>
</Project>
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 21
- Comments: 16
I stumbled on this one when I had to generate a new version of a client side-by-side.
I had a look at https://github.com/RicoSuter/NSwag/blob/85ae862fd6d68173a201a79e0ad06e0be2ec5de1/src/NSwag.CodeGeneration.CSharp/CSharpClientGeneratorSettings.cs to check the options we have.
Finally, I decided not to use a generated ApiException class, but instead move that to my own file.
I do have the same issue. Used the following workaround:
Here is the ApiException code
I just encountered this issue as well. I ended up using the workaround because I have more than one connected service in visual studio and need the other ones to build and they are all in different namespaces.
I can manually modify the code to build fine but the workaround is the only way to get this to succeed on my build server.
How to work with two or more OpenAPIs in Visual Studio 2022
The situation is that I have to add two or more OpenAPIs to a VS2022 project, because my service interfaces to more APIs.
Here is my cumbersome procedure:
<Options>/AdditionalNamespaceUsages:TheOpenApiNamespace /GenerateExceptionClasses:false</Options>. TheOpenApiNamespace is replaced with the actual namespace.When I have to maintain the OpenAPI services like extending or upgrade to a new version, then I download the OpenAPI swagger json file and overwrite the existing. Next I delete the …SwaggerClient.cs n the obj folder. VS2022 rebuilds then a new …SwaggerClient.cs.
I hope it will be easier soon!
@RyptHunterz Still no PR from you ???
@magoogli I’ve use nswag studio for this purpose… I would recommand to check it out https://github.com/RicoSuter/NSwag/wiki/NSwagStudio
The thing is that we would need a set of exception classes per generated namespace.
If we reference multiple OpenAPI services, we need to group by namespace and, either generate the exception classes in the first generated cs per namespace, or generate a separate file like
"$(namespace).Exceptions.cs"only containing the exception classes.I think it is easier, though, to assume that exception MUST always be generated, and leave the user the option to add the option
/GenerateExceptionClasses:falsewhen generating multiple clients on the same namespace.In my case I find it the common case when adding multiple OpenAPI references, because I always add the following attributes to my controller’s actions:
So, NSwag end’s up generating classes for
ProblemDetailsandValidationProblemDetailson each generated client code. This forces my to generate each client on a different namespace anyway.I prefer to leave the user with more control instead of assuming a configuration that breaks more complex code.