aspnetcore: Exception when multiple OpenAPI services refrences is added in Visual Studio 2019 (swagger endpoints)
This issue has been moved from a ticket on Developer Community.
I try to use the service reference OpenAPI in Visual Studio 2019, to generate clients based on existing swagger endpoints. It works fine when one endpoint is added, but multiple endpoints in different namespaces does not work. The reason is that the autogenerated APIException class is only generated in in the first “OpenApiReference”. I tried to set the option “generateExceptionClasses” but it didn’t have any effect. Also tried to give the exception different names with the option “exceptionClass”, but the exception class was only created in the first client.
This is the code that fails:
<ItemGroup>
<OpenApiReference Include="OpenAPIs\api1Swagger.json" CodeGenerator="NSwagCSharp" Namespace="API1">
<SourceUri>https://api1/swagger/v1/swagger.json</SourceUri>
<ClassName>{controller}Client</ClassName>
<OutputPath>API1Client.cs</OutputPath>
</OpenApiReference>
<OpenApiReference Include="OpenAPIs\api2Swagger.json" CodeGenerator="NSwagCSharp" Namespace="API2">
<SourceUri>https://api2/swagger/v1/swagger.json</SourceUri>
<ClassName>{controller}Client</ClassName>
<OutputPath>API2Client.cs</OutputPath>
</OpenApiReference>
</ItemGroup>
It’s possible to solve the problem with the following code, but I don’t like to reference to API1 in API2:
<ItemGroup>
<OpenApiReference Include="OpenAPIs\api1Swagger.json" CodeGenerator="NSwagCSharp" Namespace="API1">
<SourceUri>https://api1/swagger/v1/swagger.json</SourceUri>
<ClassName>{controller}Client</ClassName>
<OutputPath>API1Client.cs</OutputPath>
</OpenApiReference>
<OpenApiReference Include="OpenAPIs\api2Swagger.json" CodeGenerator="NSwagCSharp" Namespace="API2">
<SourceUri>https://api2/swagger/v1/swagger.json</SourceUri>
<ClassName>{controller}Client</ClassName>
<OutputPath>API2Client.cs</OutputPath>
<Options>/ExceptionClass:"API1. ApiException"</Options>
</OpenApiReference>
</ItemGroup>
Any way to slove this?
Original Comments
Visual Studio Feedback System on 12/5/2019, 00:01 AM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Visual Studio Feedback System on 12/6/2019, 01:28 AM:
Thank you for sharing your feedback! Our teams prioritize action on product issues with broad customer impact. See details at: https://docs.microsoft.com/en-us/visualstudio/ide/report-a-problem?view=vs-2017#faq. In case you need answers to common questions or need assisted support, be sure to use https://visualstudio.microsoft.com/vs/support/. We’ll keep you posted on any updates to this feedback.
Original Solutions
(no solutions)
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 5
- Comments: 19 (3 by maintainers)
Move
ApiExceptionandApiException<TResult>to their own namespaceModify your csproj file OpenApiReference elements to include
<Options>/AdditionalNamespaceUsages:Ent.Api.Exceptions /GenerateExceptionClasses:false</Options>2024 and this is still an issue.
Hi Team.
This really is quite a big issue for users.
The case for a microservice referencing more than one other microservice is very common, so a lot of people will encounter this (myself included).
Please can you re-triage it with this in mind?
I am running into this problem also when having greater than 2 OpenAPI specs in a single Project. I have them configured to use different Namespaces and different client names same
The above work around does work which gets me by for now but I would hope this gets resolved at some point.
cool, so basically “Example 3: Every client gets its own generated ApiException class (in different namespaces).” should be the case. And this is a MUST when you are using a different namespaces.
Apparently the order is also important, it has to be at the beginning of the options element, e.g:
<Options>/AdditionalNamespaceUsages:Buddy.App.Services.OpenAPIs /GenerateExceptionClasses:false /UseBaseUrl:false /GenerateClientInterfaces:true /DateType:System.DateTime /DateTimeType:System.DateTime</Options>I wasted a lot of time trying to understand why it doesn’t add additional namespace
We’ve moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
There is a good write-up here on how to make this work (assuming you have an up-to-date NSwag): https://github.com/RicoSuter/NSwag/issues/4938#issuecomment-2236419139
I had to create a different Exception class to get this to work:
Also note that
/AdditionalNamespaceUsages:Ent.Api.Exceptions, the Ent.Api.Exceptions is not a copy and paster - you need to use your own namspace for the class you’ve created e.g.MyApp.OpenApi.Exceptions.The ApiException error exists when generating two schemas: The type or namespace name ‘ApiException<>’ could not be found (are you missing a using directive or an assembly reference?)
When will it be fixed?