swagger-api / swagger-api/swagger-codegen

Enhance configurability of the OkHttpClient used by Java+retrofit2

Open
#9,002 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The class ApiClient generated for Java+retrofit2 is not easily configurable.
The default constructor of the class calls the method createDefaultAdapter().
This method configures the "adapter builder" that is later used go create the services as shown below:

  public void createDefaultAdapter() {
    json = new JSON();
    okBuilder = new OkHttpClient.Builder();

    String baseUrl = "https://localhost/someco/contracts-api";
    if (!baseUrl.endsWith("/"))
      baseUrl = baseUrl + "/";

    adapterBuilder = new Retrofit
      .Builder()
      .baseUrl(baseUrl)
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
      .addConverterFactory(GsonCustomConverterFactory.create(json.getGson()));
  }

  public <S> S createService(Class<S> serviceClass) {
    return adapterBuilder
      .client(okBuilder.build())
      .build()
      .create(serviceClass);
  }
Swagger-codegen version

swagger-codegen-maven-plugin 2.3.1

Command line used for generation
<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <output>${project.basedir}</output>
                <!-- specify the swagger yaml -->
                <inputSpec>${project.basedir}/api-editor/api/swagger/swagger.yaml</inputSpec>
                <!-- target to generate java client code -->
                <language>java</language>
                <generateApiTests>true</generateApiTests>
                <generateModelTests>true</generateModelTests>
                <configOptions>
                    <dateLibrary>joda</dateLibrary>
                    <invokerPackage>com.someco.client.invoker</invokerPackage>
                    <apiPackage>com.someco.client.api</apiPackage>
                    <modelPackage>com.someco.client.model</modelPackage>
                    <sourceFolder>src/gen/java</sourceFolder>
                    <serializableModel>true</serializableModel>
                    <hideGenerationTimestamp>true</hideGenerationTimestamp>
                </configOptions>
                <!-- override the default library to retrofit2 -->
                <library>retrofit2</library>
            </configuration>
        </execution>
    </executions>
</plugin>

Steps to reproduce

Generate the client code using maven and the configuration above

Suggest a fix/enhancement

With the current implementation it seems impossible, for example, to configure a reusable OkHttpClient that is configured with a proper connection pool.
Because the method createDefaultAdapter() needs to configure private members, it cannot be overridden by a super class and so its behaviour cannot be modified using inheritance.

Couldn't be better to make it possible to execute a code like:

ConnectionPool pool = new ConnectionPool(poolSize, keepAlive, TimeUnit.SECONDS);
OkHttpClient client = apiClient.getOkBuilder().connectionPool(pool).build();
apiClient.getAdapterBuilder().baseUrl(apiPath).client(client);
apiClient.build(); // this builds the adapter (a "Retrofit" object) using the adapterBuilder.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Java retrofit2 generation entry points that produce ApiClient, then review createDefaultAdapter() and createService() to understand the current builder lifecycle. The work is complete when generated clients allow callers to configure a reusable OkHttpClient and Retrofit adapter without modifying generated code; the issue does not name specific template files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.