Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #3497
Closed
Open
Issue created Jul 30, 2019 by Administrator@rootContributor

[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()

Assignee
Assign to
Time tracking