swagger-api / swagger-api/swagger-codegen
[java] ApiClient library limits. New features.
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:
- 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
- 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
- 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
OkHttpandGsonto 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
buildDefaultOkHttponly 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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