Fix for issue #5777 - java/jersey2 ApiClient.invokeAPI doesn´t close the Response object (#5782)

* Update ApiClient.mustache

Fix for issue #5777

* Update ApiClient.mustache

* Create ApiClient.mustache

* Run java-petstore-jersey2.sh
This commit is contained in:
Moscagus 2017-06-14 13:45:14 -03:00 committed by wing328
parent 77c14bca39
commit f2276e5b35
2 changed files with 96 additions and 80 deletions

View File

@ -682,48 +682,56 @@ public class ApiClient {
Entity<?> entity = serialize(body, formParams, contentType); Entity<?> entity = serialize(body, formParams, contentType);
Response response; Response response = null;
if ("GET".equals(method)) { try {
response = invocationBuilder.get(); if ("GET".equals(method)) {
} else if ("POST".equals(method)) { response = invocationBuilder.get();
response = invocationBuilder.post(entity); } else if ("POST".equals(method)) {
} else if ("PUT".equals(method)) { response = invocationBuilder.post(entity);
response = invocationBuilder.put(entity); } else if ("PUT".equals(method)) {
} else if ("DELETE".equals(method)) { response = invocationBuilder.put(entity);
response = invocationBuilder.delete(); } else if ("DELETE".equals(method)) {
} else if ("PATCH".equals(method)) { response = invocationBuilder.delete();
response = invocationBuilder.header("X-HTTP-Method-Override", "PATCH").post(entity); } else if ("PATCH".equals(method)) {
} else { response = invocationBuilder.header("X-HTTP-Method-Override", "PATCH").post(entity);
throw new ApiException(500, "unknown method type " + method); } else {
} throw new ApiException(500, "unknown method type " + method);
}
statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response); statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response);
if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
return null; if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
} else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) { return null;
if (returnType == null) } else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) {
return null; if (returnType == null)
else return null;
return deserialize(response, returnType); else
} else { return deserialize(response, returnType);
String message = "error"; } else {
String respBody = null; String message = "error";
if (response.hasEntity()) { String respBody = null;
try { if (response.hasEntity()) {
respBody = String.valueOf(response.readEntity(String.class)); try {
message = respBody; respBody = String.valueOf(response.readEntity(String.class));
} catch (RuntimeException e) { message = respBody;
// e.printStackTrace(); } catch (RuntimeException e) {
} // e.printStackTrace();
}
}
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
}
} finally {
try {
response.close();
} catch (Exception e) {
// it's not critical, since the response object is local in method invokeAPI; that's fine, just continue
} }
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
} }
} }

View File

@ -671,48 +671,56 @@ public class ApiClient {
Entity<?> entity = serialize(body, formParams, contentType); Entity<?> entity = serialize(body, formParams, contentType);
Response response; Response response = null;
if ("GET".equals(method)) { try {
response = invocationBuilder.get(); if ("GET".equals(method)) {
} else if ("POST".equals(method)) { response = invocationBuilder.get();
response = invocationBuilder.post(entity); } else if ("POST".equals(method)) {
} else if ("PUT".equals(method)) { response = invocationBuilder.post(entity);
response = invocationBuilder.put(entity); } else if ("PUT".equals(method)) {
} else if ("DELETE".equals(method)) { response = invocationBuilder.put(entity);
response = invocationBuilder.delete(); } else if ("DELETE".equals(method)) {
} else if ("PATCH".equals(method)) { response = invocationBuilder.delete();
response = invocationBuilder.header("X-HTTP-Method-Override", "PATCH").post(entity); } else if ("PATCH".equals(method)) {
} else { response = invocationBuilder.header("X-HTTP-Method-Override", "PATCH").post(entity);
throw new ApiException(500, "unknown method type " + method); } else {
} throw new ApiException(500, "unknown method type " + method);
}
statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response); statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response);
if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
return null; if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
} else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) { return null;
if (returnType == null) } else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) {
return null; if (returnType == null)
else return null;
return deserialize(response, returnType); else
} else { return deserialize(response, returnType);
String message = "error"; } else {
String respBody = null; String message = "error";
if (response.hasEntity()) { String respBody = null;
try { if (response.hasEntity()) {
respBody = String.valueOf(response.readEntity(String.class)); try {
message = respBody; respBody = String.valueOf(response.readEntity(String.class));
} catch (RuntimeException e) { message = respBody;
// e.printStackTrace(); } catch (RuntimeException e) {
} // e.printStackTrace();
}
}
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
}
} finally {
try {
response.close();
} catch (Exception e) {
// it's not critical, since the response object is local in method invokeAPI; that's fine, just continue
} }
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
} }
} }