openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [csharp][generichost] Duplicit HTTP client registrations in factory

Open McMlok opened this issue 1 year ago • 2 comments

Description

In our organization two teams provide API to work with comments. Both add tag Comment to endpoint. They both generate csharp client. When another team need to integrate to both APIs an application is unable to start with error

The HttpClient factory already has a registered client with the name 'ICommentApi', bound to the type 'Test1.Client.Api.ICommentApi'. Client names are computed based on the type name without considering the namespace ('ICommentApi'). Use an overload of AddHttpClient that accepts a string and provide a unique name to resolve the conflict.

openapi-generator version

7.16.0

OpenAPI declaration file content or url

Two API definitions are here

Generation Details
Steps to reproduce
  • Create project for Test1 definition
  • Create project for Test2 definition
  • Create another project and reference projects generated from Test1 and Test2
  • Add both AddApi method to service collection.
  • Run the application
Suggest a fix

Add name when AddHttpClient called in .\main\resources\csharp\libraries\generichost\HostConfiguration.mustache for example

{{#apiInfo}}{{#apis}}builders.Add(_services.AddHttpClient<{{interfacePrefix}}{{classname}}, {{classname}}>("{{packageName}}.{{apiPackage}}.{{interfacePrefix}}{{classname}}", client));
            {{/apis}}{{/apiInfo}}

McMlok avatar Oct 06 '25 11:10 McMlok

thanks for opening an issue

do you mind filing a PR with the suggested fix to start with?

wing328 avatar Oct 06 '25 17:10 wing328

I had related issue ( AddHttpClient cannot register two api with same name ) so I used --additional-properties interfacePrefix=ISome to disambiguate

I then discovered ( used openapi-generator-cli 7.19.0 ) that api not applied the prefix in followings parts of code:

applying changes manually makes it works elsewhere compile time errors appears.

devel0 avatar Jan 24 '26 23:01 devel0