fix empty body in java apache client (#14574)

This commit is contained in:
William Cheng 2023-02-01 15:41:55 +08:00 committed by GitHub
parent 8d9816e193
commit 32499a19fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View File

@ -830,7 +830,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
String mimeType = getResponseMimeType(response);
if (mimeType == null || isJsonMime(mimeType)) {
// Assume json if no mime type
return objectMapper.readValue(entity.getContent(), valueType);
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
String content = (String) (s.hasNext() ? s.next() : "");
if ("".equals(content)) { // returns null for empty body
return null;
}
return objectMapper.readValue(content, valueType);
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
@ -1069,7 +1077,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
}
} else {
// for empty body
builder.setEntity(serialize(null, null, contentTypeObj));
builder.setEntity(new StringEntity("", contentTypeObj));
}
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {

View File

@ -703,7 +703,15 @@ public class ApiClient extends JavaTimeFormatter {
String mimeType = getResponseMimeType(response);
if (mimeType == null || isJsonMime(mimeType)) {
// Assume json if no mime type
return objectMapper.readValue(entity.getContent(), valueType);
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
String content = (String) (s.hasNext() ? s.next() : "");
if ("".equals(content)) { // returns null for empty body
return null;
}
return objectMapper.readValue(content, valueType);
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
@ -942,7 +950,7 @@ public class ApiClient extends JavaTimeFormatter {
}
} else {
// for empty body
builder.setEntity(serialize(null, null, contentTypeObj));
builder.setEntity(new StringEntity("", contentTypeObj));
}
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {

View File

@ -846,7 +846,15 @@ public class ApiClient extends JavaTimeFormatter {
String mimeType = getResponseMimeType(response);
if (mimeType == null || isJsonMime(mimeType)) {
// Assume json if no mime type
return objectMapper.readValue(entity.getContent(), valueType);
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
String content = (String) (s.hasNext() ? s.next() : "");
if ("".equals(content)) { // returns null for empty body
return null;
}
return objectMapper.readValue(content, valueType);
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
@ -1085,7 +1093,7 @@ public class ApiClient extends JavaTimeFormatter {
}
} else {
// for empty body
builder.setEntity(serialize(null, null, contentTypeObj));
builder.setEntity(new StringEntity("", contentTypeObj));
}
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {