Aspnetcore: Exception when multiple OpenAPI services refrences is added in Visual Studio 2019 (swagger endpoints)

Created on 8 Jan 2020  路  2Comments  路  Source: dotnet/aspnetcore

_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鈥檒l keep you posted on any updates to this feedback.


Original Solutions

(no solutions)

affected-few area-mvc bug severity-minor

Most helpful comment

Move ApiException and ApiException<TResult> to their own namespace

namespace Company.Api.Exceptions
{
  public partial class ApiException : System.Exception
  {
    ...
  }
  public partial class ApiException<TResult> : ApiException
  {
    ...
  }
}

Modify your csproj file OpenApiReference elements to include <Options>/AdditionalNamespaceUsages:Ent.Api.Exceptions /GenerateExceptionClasses:false</Options>

```


https://api1/swagger/v1/swagger.json
{controller}Client
API1Client.cs
/AdditionalNamespaceUsages:Ent.Api.Exceptions /GenerateExceptionClasses:false


https://api2/swagger/v1/swagger.json
{controller}Client
API2Client.cs
/AdditionalNamespaceUsages:Ent.Api.Exceptions /GenerateExceptionClasses:false


````

All 2 comments

Move ApiException and ApiException<TResult> to their own namespace

namespace Company.Api.Exceptions
{
  public partial class ApiException : System.Exception
  {
    ...
  }
  public partial class ApiException<TResult> : ApiException
  {
    ...
  }
}

Modify your csproj file OpenApiReference elements to include <Options>/AdditionalNamespaceUsages:Ent.Api.Exceptions /GenerateExceptionClasses:false</Options>

```


https://api1/swagger/v1/swagger.json
{controller}Client
API1Client.cs
/AdditionalNamespaceUsages:Ent.Api.Exceptions /GenerateExceptionClasses:false


https://api2/swagger/v1/swagger.json
{controller}Client
API2Client.cs
/AdditionalNamespaceUsages:Ent.Api.Exceptions /GenerateExceptionClasses:false


````

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.

Was this page helpful?
0 / 5 - 0 ratings