Skip to content

[Java][okhttp-gson] Fix incorrect use of OkHttp interceptors #8053

Open
ngaya-ll wants to merge 6 commits intoswagger-api:masterfrom
ngaya-ll:issue-7876
Open

[Java][okhttp-gson] Fix incorrect use of OkHttp interceptors #8053
ngaya-ll wants to merge 6 commits intoswagger-api:masterfrom
ngaya-ll:issue-7876

Conversation

@ngaya-ll
Copy link

@ngaya-ll ngaya-ll commented Apr 21, 2018

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: 3.0.0 branch for changes related to OpenAPI spec 3.0. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language. @bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger

Description of the PR

Currently, the generated Java okhttp-gson client adds an interceptor to the underlying OkHttpClient for each async call. The purpose of the interceptor is to wrap the response body to track download progress. This implementation doesn't work correctly, for multiple reasons:

  • The interceptor intercepts all requests to the client, not just the one it's trying to track.
  • Interceptors are never removed from the client, so each async request adds another layer of interception for all subsequent requests. Since the interceptor chain is invoked recursively, at some point all requests will start failing with a StackOverflowError.

With this code change, the client uses a single interceptor to decorate all async requests and send updates to the relevant listener only.

I also added a test case to demonstrate the issue, which fails on the current master and passes on this branch.

Fixes #7876, #8915

result.put("pet", pet);
}
@Test
public void testCreateAndGetMultiplePetsAsync() throws Exception {
Copy link
Author

@ngaya-ll ngaya-ll Apr 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new test mentioned in the description. I also modified the preceding async test.

}

@Test
@Ignore
Copy link
Author

@ngaya-ll ngaya-ll Apr 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This endpoint is deprecated on petstore.swagger.io and was returning a 500 status.

*/
public ApiClient setHttpClient(OkHttpClient httpClient) {
this.httpClient = httpClient;
addProgressInterceptor();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if multiple ApiClients are created with the same underlying OkHttpClient this will create duplicate progress updates for each request. However, from reading the rest of the class (e.g. the setDebugging() method) I concluded that this is not a supported use case.

@ngaya-ll
Copy link
Author

ngaya-ll commented May 18, 2018

@bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger Any update on this? This fixes a critical bug for users making async calls using okhttp-gson generated clients.

@mattnworb
Copy link

It would be lovely to have this merged to fix #7876 as the current state renders it impossible to use any of the generated "async" methods in any sort of long-lived production server.

@mtnourji
Copy link

It would be lovely to have this merged to fix #7876 as the current state renders it impossible to use any of the generated "async" methods in any sort of long-lived production server.

+1
Any update on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants

Comments