Skip to content
Permalink
Browse files

Fix for the added interceptor and when the http client is set several…

… times, which leads to a stack overflow because of too many interceptors. (#2420)

Also added tests for that.
  • Loading branch information...
burberius authored and wing328 committed Mar 18, 2019
1 parent 9bb94fe commit 82700e5958fd6d48c23d529f74538f8ee290bb34
@@ -1,5 +1,6 @@
package org.openapitools.client;

import okhttp3.OkHttpClient;
import org.openapitools.client.auth.*;

import java.text.DateFormat;
@@ -165,7 +166,7 @@ public void testGetAndSetConnectTimeout() {

apiClient.setConnectTimeout(10000);
}

@Test
public void testGetAndSetReadTimeout() {
// read timeout defaults to 10 seconds
@@ -178,7 +179,7 @@ public void testGetAndSetReadTimeout() {

apiClient.setReadTimeout(10000);
}

@Test
public void testGetAndSetWriteTimeout() {
// write timeout defaults to 10 seconds
@@ -191,7 +192,7 @@ public void testGetAndSetWriteTimeout() {

apiClient.setWriteTimeout(10000);
}

@Test
public void testParameterToPairWhenNameIsInvalid() throws Exception {
List<Pair> pairs_a = apiClient.parameterToPair(null, new Integer(1));
@@ -327,4 +328,25 @@ public void testSanitizeFilename() {
assertEquals("sun.gif", apiClient.sanitizeFilename("c:\\var\\tmp\\sun.gif"));
assertEquals("sun.gif", apiClient.sanitizeFilename(".\\sun.gif"));
}


@Test
public void testInterceptorCleanupWithNewClient() {
OkHttpClient oldClient = apiClient.getHttpClient();
assertEquals(1, oldClient.networkInterceptors().size());

OkHttpClient newClient = new OkHttpClient();
apiClient.setHttpClient(newClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
apiClient.setHttpClient(newClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
}

@Test
public void testInterceptorCleanupWithSameClient() {
OkHttpClient oldClient = apiClient.getHttpClient();
assertEquals(1, oldClient.networkInterceptors().size());
apiClient.setHttpClient(oldClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
}
}
@@ -137,7 +137,7 @@ public class ApiClient {
{{/hasOAuthMethods}}
private void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(getProgressInterceptor());
builder.addNetworkInterceptor(getProgressInterceptor());
httpClient = builder.build();

{{#useGzipFeature}}
@@ -187,11 +187,22 @@ public class ApiClient {
/**
* Set HTTP client
*
* @param httpClient An instance of OkHttpClient
* @param newHttpClient An instance of OkHttpClient
* @return Api Client
*/
public ApiClient setHttpClient(OkHttpClient httpClient) {
this.httpClient = httpClient.newBuilder().addInterceptor(getProgressInterceptor()).build();
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
if(!httpClient.equals(newHttpClient)) {
OkHttpClient.Builder builder = newHttpClient.newBuilder();
Iterator<Interceptor> networkInterceptorIterator = httpClient.networkInterceptors().iterator();
while(networkInterceptorIterator.hasNext()) {
builder.addNetworkInterceptor(networkInterceptorIterator.next());
}
Iterator<Interceptor> interceptorIterator = httpClient.interceptors().iterator();
while(interceptorIterator.hasNext()) {
builder.addInterceptor(interceptorIterator.next());
}
this.httpClient = builder.build();
}
return this;
}

@@ -99,10 +99,10 @@ if(hasProperty('target') && target == 'android') {

dependencies {
compile 'io.swagger:swagger-annotations:1.5.21'
compile 'com.squareup.okhttp3:okhttp:3.12.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.12.1'
compile 'com.squareup.okhttp3:okhttp:3.13.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.13.1'
compile 'com.google.code.gson:gson:2.8.5'
compile 'io.gsonfire:gson-fire:1.8.0'
compile 'io.gsonfire:gson-fire:1.8.3'
compile group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
compile 'org.threeten:threetenbp:1.3.5'
@@ -10,13 +10,13 @@ lazy val root = (project in file(".")).
resolvers += Resolver.mavenLocal,
libraryDependencies ++= Seq(
"io.swagger" % "swagger-annotations" % "1.5.21",
"com.squareup.okhttp3" % "okhttp" % "3.12.1",
"com.squareup.okhttp3" % "logging-interceptor" % "3.12.1",
"com.squareup.okhttp3" % "okhttp" % "3.13.1",
"com.squareup.okhttp3" % "logging-interceptor" % "3.13.1",
"com.google.code.gson" % "gson" % "2.8.5",
"org.apache.commons" % "commons-lang3" % "3.8.1",
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1",
"org.threeten" % "threetenbp" % "1.3.5" % "compile",
"io.gsonfire" % "gson-fire" % "1.8.0" % "compile",
"io.gsonfire" % "gson-fire" % "1.8.3" % "compile",
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.10" % "test"
)
@@ -238,9 +238,9 @@
<java.version>1.7</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
<gson-fire-version>1.8.3</gson-fire-version>
<swagger-core-version>1.5.21</swagger-core-version>
<okhttp-version>3.12.1</okhttp-version>
<okhttp-version>3.13.1</okhttp-version>
<gson-version>2.8.5</gson-version>
<commons-lang3-version>3.8.1</commons-lang3-version>
<threetenbp-version>1.3.5</threetenbp-version>
@@ -123,7 +123,7 @@ public ApiClient(String clientId, String clientSecret, Map<String, String> param

private void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(getProgressInterceptor());
builder.addNetworkInterceptor(getProgressInterceptor());
httpClient = builder.build();


@@ -169,11 +169,22 @@ public OkHttpClient getHttpClient() {
/**
* Set HTTP client
*
* @param httpClient An instance of OkHttpClient
* @param newHttpClient An instance of OkHttpClient
* @return Api Client
*/
public ApiClient setHttpClient(OkHttpClient httpClient) {
this.httpClient = httpClient.newBuilder().addInterceptor(getProgressInterceptor()).build();
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
if(!httpClient.equals(newHttpClient)) {
OkHttpClient.Builder builder = newHttpClient.newBuilder();
Iterator<Interceptor> networkInterceptorIterator = httpClient.networkInterceptors().iterator();
while(networkInterceptorIterator.hasNext()) {
builder.addNetworkInterceptor(networkInterceptorIterator.next());
}
Iterator<Interceptor> interceptorIterator = httpClient.interceptors().iterator();
while(interceptorIterator.hasNext()) {
builder.addInterceptor(interceptorIterator.next());
}
this.httpClient = builder.build();
}
return this;
}

@@ -0,0 +1,60 @@
/*
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


package org.openapitools.client.auth;

import org.openapitools.client.Pair;

import java.util.Map;
import java.util.List;


public class HttpBearerAuth implements Authentication {
private final String scheme;
private String bearerToken;

public HttpBearerAuth(String scheme) {
this.scheme = scheme;
}

/**
* Gets the token, which together with the scheme, will be sent as the value of the Authorization header.
*
* @return The bearer token
*/
public String getBearerToken() {
return bearerToken;
}

/**
* Sets the token, which together with the scheme, will be sent as the value of the Authorization header.
*
* @param bearerToken The bearer token to send in the Authorization header
*/
public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
}

@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if(bearerToken == null) {
return;
}

headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken);
}

private static String upperCaseBearer(String scheme) {
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
}
}
@@ -0,0 +1,51 @@
/*
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


package org.openapitools.client.model;

import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;


/**
* Model tests for ModelReturn
*/
public class ModelReturnTest {
private final ModelReturn model = new ModelReturn();

/**
* Model tests for ModelReturn
*/
@Test
public void testModelReturn() {
// TODO: test ModelReturn
}

/**
* Test the property '_return'
*/
@Test
public void _returnTest() {
// TODO: test _return
}

}
@@ -125,7 +125,7 @@ public ApiClient(String clientId, String clientSecret, Map<String, String> param

private void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(getProgressInterceptor());
builder.addNetworkInterceptor(getProgressInterceptor());
httpClient = builder.build();


@@ -171,11 +171,22 @@ public OkHttpClient getHttpClient() {
/**
* Set HTTP client
*
* @param httpClient An instance of OkHttpClient
* @param newHttpClient An instance of OkHttpClient
* @return Api Client
*/
public ApiClient setHttpClient(OkHttpClient httpClient) {
this.httpClient = httpClient.newBuilder().addInterceptor(getProgressInterceptor()).build();
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
if(!httpClient.equals(newHttpClient)) {
OkHttpClient.Builder builder = newHttpClient.newBuilder();
Iterator<Interceptor> networkInterceptorIterator = httpClient.networkInterceptors().iterator();
while(networkInterceptorIterator.hasNext()) {
builder.addNetworkInterceptor(networkInterceptorIterator.next());
}
Iterator<Interceptor> interceptorIterator = httpClient.interceptors().iterator();
while(interceptorIterator.hasNext()) {
builder.addInterceptor(interceptorIterator.next());
}
this.httpClient = builder.build();
}
return this;
}

@@ -125,7 +125,7 @@ public ApiClient(String clientId, String clientSecret, Map<String, String> param

private void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(getProgressInterceptor());
builder.addNetworkInterceptor(getProgressInterceptor());
httpClient = builder.build();


@@ -171,11 +171,22 @@ public OkHttpClient getHttpClient() {
/**
* Set HTTP client
*
* @param httpClient An instance of OkHttpClient
* @param newHttpClient An instance of OkHttpClient
* @return Api Client
*/
public ApiClient setHttpClient(OkHttpClient httpClient) {
this.httpClient = httpClient.newBuilder().addInterceptor(getProgressInterceptor()).build();
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
if(!httpClient.equals(newHttpClient)) {
OkHttpClient.Builder builder = newHttpClient.newBuilder();
Iterator<Interceptor> networkInterceptorIterator = httpClient.networkInterceptors().iterator();
while(networkInterceptorIterator.hasNext()) {
builder.addNetworkInterceptor(networkInterceptorIterator.next());
}
Iterator<Interceptor> interceptorIterator = httpClient.interceptors().iterator();
while(interceptorIterator.hasNext()) {
builder.addInterceptor(interceptorIterator.next());
}
this.httpClient = builder.build();
}
return this;
}

Oops, something went wrong.

0 comments on commit 82700e5

Please sign in to comment.
You can’t perform that action at this time.