Skip to content

[REQ] Extend JavaSpring apiClient.mustache with option to override contextId for FeignClient #20941

@paveliam

Description

@paveliam

Is your feature request related to a problem? Please describe.

When you try to generate JavaSpring FeignClients for the same service but from different API specs (e.g. 1st spec for Internal API, 2nd one for Admin API), you will get an error on load ApplicationContext as there is a conflict in aliases which are generated from the same service name.

Caused by: java.lang.IllegalStateException: Cannot define alias 'service-nameFeignClient' for name 'com.example.api.MyServiceInternalApiClient': It is already registered for name 'com.example.api.MyServiceAdminApiClient'.
	at org.springframework.core.SimpleAliasRegistry.registerAlias(SimpleAliasRegistry.java:73)
	at org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerBeanDefinition(BeanDefinitionReaderUtils.java:170)

Describe the solution you'd like

I propose to extend

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-cloud/apiClient.mustache#L6C90-L6C111

with an option to define contextId as well. By default it can be equal to name.
By specifying contextId for FeignClient there won't be a conflict in aliases.

Here is how it may look like:

package {{package}};

import org.springframework.cloud.openfeign.FeignClient;
import {{configPackage}}.ClientConfiguration;

@FeignClient(
    name="${{openbrace}}{{classVarName}}.name:{{classVarName}}{{closebrace}}",
    {{#useFeignClientContextId}}contextId="${{openbrace}}{{classVarName}}.contextId:${{openbrace}}{{classVarName}}.name{{closebrace}}{{closebrace}}",{{/useFeignClientContextId}}
    {{#useFeignClientUrl}}url="${{openbrace}}{{classVarName}}.url:{{basePath}}{{closebrace}}",{{/useFeignClientUrl}}
    configuration = ClientConfiguration.class
)
public interface {{classname}}Client extends {{classname}} {
}

Describe alternatives you've considered

As a workaround I have to create manually feign clients if there are such conflicts which is annoying.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions