swagger-api / swagger-api/swagger-codegen

[Java][okhttp-gson] Generated APIs do not clean com.squareup.okhttp.OkHttpClient#networkInterceptors

Open
#7,876 3 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

OKHttp documentation states that OkHttpClient instance should be shared and reused
(https://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/OkHttpClient.html)

But if we reuse swagger-codegen generated API (or APIClient), every async call with callback will add
new networkInterceptor
(see
apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor()
in generated API code)

This behavior does not allow to reuse APIClient or *API instance.

Swagger-codegen version

2.3.1

Swagger declaration file content or url

https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/yaml/petstore-minimal.yaml

Command line used for generation

Online client generator at https://editor.swagger.io/ , java client

Steps to reproduce
  1. get https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/yaml/petstore-minimal.yaml yaml file
  2. Go to https://editor.swagger.io/ and generate java client for that yaml
  3. Look into generated zip
    package io.swagger.client.api;
    ..
    public com.squareup.okhttp.Call petsGetCall(
    ...
    if(progressListener != null) {
    apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
    @Override
    public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException {
    com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
    return originalResponse.newBuilder()
    .body(new ProgressResponseBody(originalResponse.body(), progressListener))
    .build();
    }
    });
    }
    ....
Related issues/PRs
Suggest a fix/enhancement

Those global networkInterceptors are used for ApiCallback#onDownloadProgress callback method, other callback methods are invoked with ProgressRequestBody.ProgressRequestListener assigned to individual request.
Is support of onDownloadProgress callback method required ?

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 with the generated Java API call methods containing apiClient.getHttpClient().networkInterceptors().add and review how ApiCallback#onDownloadProgress is handled. Reproduce the petstore-minimal.yaml client through the online generator, then verify that reusing an APIClient or API instance does not accumulate network interceptors while the documented progress callback behavior remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
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.