[BUG] [JAVA] okhttp-gson generates broken method in ApiClient.java
Created by: lostiniceland
Description
The current Okhttp3 - ApiClient Mustache generates a broken method.
the current setHttpClient(OkHttpClient newHttpClient)
copies the interceptors from the current field to the new variable, ignoring the fact that the interceptors are already present.
The result is, that each call to setHttpClient duplicates the current interceptors.
Okhttp made the HttpClient immutable and provided a builder-pattern which already captures the current state.
openapi-generator version
4.0.2
Steps to reproduce
- Configure Generator to use okhttp-gson
- Generate some API
- Prepare ApiClient
- Add Interceptor
apiClient.setHttpClient(apiClient.getHttpClient().newBuilder().addInterceptor(interceptor).build())
- Repeat the previous call and check the Interceptors (the first one is now duplicated)
Suggest a fix
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
this.httpClient = newHttpClient;
}
Workaround with Groovy
We faced this problem in our Spock-Tests (Groovy) and thanks to Groovy we can bypass the setHttpClient method and set the new client directly using the @-Syntax
apiClient.@httpClient = apiClient.httpClient.newBuilder().addInterceptor(interceptor).build()