swagger-api / swagger-api/swagger-codegen

[java] ApiClient library limits. New features.

Open
#4,213 0 comments 1 reaction 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

I faced with few critical(for me) limits of ApiClient generated by codegen.
I have 3 different retroft services in project, also I have another network modules like Fresco, Glide and custom raw http requests.

I want these features in generated code:

  1. Ability of use one instance of HTTP client inside project.
    ApiClient creates new Okhttp client for every retrofit service creating. And I can't specify external instance.
public <S> S createService(Class<S> serviceClass) {
        return adapterBuilder
            .client(okBuilder.build())
            .build()
            .create(serviceClass);

    }
I want ability to set my own instance of OkHttpClicnet
  1. Ability of use one instance of Gson in project;
    ApiClient creates gson wile builds retrofit adapter. I want to set my gson also.
 public void createDefaultAdapter() {
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                .registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
                .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
                .create();
    //creating of adapterBuilder
  1. Using one retrofit instance for every service;
    Now ApiClient creates new instance of retrofit for every service build.
public <S> S createService(Class<S> serviceClass) {
        return adapterBuilder
            .client(okBuilder.build())
            .build()
            .create(serviceClass);

    }
Swagger-codegen version

2.2.1

Command line used for generation
java -jar swagger-codegen-cli.jar generate \
  -i api.yml \
  -l java \
  -o api_swagger \
  -c config.json 

{
  "invokerPackage": "com.***.***.api",
  "library": "retrofit2",
  "useRxJava": true
}
Steps to reproduce

generate any ApiClient with few retrofit interfaces.

Suggest a Fix
  • Move creating of OkHttp and Gson to public functions. So user can create it outside of client and use in another cases.
fun buildDefaultOkHttp(): OkHttpClient {
//creating OkHttp following protocol
  • Allow user to set custom OkHttpClient, Gson. Use default builders like buildDefaultOkHttp only when custom = null.
  • Creating strategy of generating large objects like okhhtp, gson, retrofit
enum FactoryStrategy {
    singleton, //use one instance of class like okhttp client 
    newInstance // create new instance for every service call
}

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

Inspect the generated ApiClient, especially createService and createDefaultAdapter, to trace where OkHttp, Gson, and Retrofit instances are created. Define how custom instances and the singleton/newInstance strategy should be exposed, then verify generated services can reuse supplied instances without losing the current defaults.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.