Eric
Eric

Reputation: 1068

Visual Studio 2019 - Connected Service Reference - OpenAPI is generating duplicate types

I am trying to generate an OpenApi service reference in Visual Studio 2019. .Net 5.0.

Right clicking project >Add>Connected Services>+ Service References

I am using NetDocs api "https://api.vault.netvoyage.com/v2/swagger/docs/v2".

Result: I get generated c# client code but it is duplicating the types with the errors below.

Severity    Code    Description Project File    Line    Suppression State
Error   CS0102  The type 'v2Client' already contains a definition for '_settings'   OpenAPITest C:\Users\dryfus\source\repos\OpenAPITest\obj\v2Client.cs    4941    Active
Error   CS0579  Duplicate 'System.CodeDom.Compiler.GeneratedCode' attribute OpenAPITest C:\Users\dryfus\source\repos\OpenAPITest\obj\v2Client.cs    4936    Active
Error   CS0102  The type 'v2Client' already contains a definition for '_baseUrl'    OpenAPITest C:\Users\dryfus\source\repos\OpenAPITest\obj\v2Client.cs    4939    Active
Error   CS0102  The type 'v2Client' already contains a definition for '_httpClient' OpenAPITest C:\Users\dryfus\source\repos\OpenAPITest\obj\v2Client.cs    4940    Active

Is there a way I can get this to work with the connector way without the duplicates? Or even cli? Any suggestions on why it is creating the duplicates?

Upvotes: 6

Views: 2601

Answers (3)

maxisam
maxisam

Reputation: 22705

It is actually caused by this option

OperationGenerationMode:MultipleClientsFromOperationId

  • MultipleClientsFromOperationId : Multiple clients from the Swagger operation ID in the form '{controller}_{action}'

However, if your classname is set as a static name, it will cause an error like you have.

The correct solution should be change the classname to something like

ClassName="{controller}ServiceClient"

So it will generate different classes.

Upvotes: 4

Miguel
Miguel

Reputation: 199

I had the same issue, it turns out the code generation didn't like enpoints with an underscore in its operationId. Lucky for me, the service I was consuming was also part of our code, so I could just go to the Swagger configuration in the API side and change the CustomOperationIds setting.

Upvotes: 5

Eric
Eric

Reputation: 1068

I downloaded and use NSwagStudio to generate the client code and did not have the same issue that the Visual Studio connector had when generating the code.

https://github.com/RicoSuter/NSwag/wiki/NSwagStudio

Upvotes: 3

Related Questions