Skip to content

Commit 32499a1

Browse files
authored
fix empty body in java apache client (#14574)
1 parent 8d9816e commit 32499a1

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

  • modules/openapi-generator/src/main/resources/Java/libraries/apache-httpclient
  • samples/client
    • echo_api/java/apache-httpclient/src/main/java/org/openapitools/client
    • petstore/java/apache-httpclient/src/main/java/org/openapitools/client

modules/openapi-generator/src/main/resources/Java/libraries/apache-httpclient/ApiClient.mustache

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
830830
String mimeType = getResponseMimeType(response);
831831
if (mimeType == null || isJsonMime(mimeType)) {
832832
// Assume json if no mime type
833-
return objectMapper.readValue(entity.getContent(), valueType);
833+
// convert input stream to string
834+
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
835+
String content = (String) (s.hasNext() ? s.next() : "");
836+
837+
if ("".equals(content)) { // returns null for empty body
838+
return null;
839+
}
840+
841+
return objectMapper.readValue(content, valueType);
834842
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
835843
// convert input stream to string
836844
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
@@ -1069,7 +1077,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
10691077
}
10701078
} else {
10711079
// for empty body
1072-
builder.setEntity(serialize(null, null, contentTypeObj));
1080+
builder.setEntity(new StringEntity("", contentTypeObj));
10731081
}
10741082

10751083
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {

samples/client/echo_api/java/apache-httpclient/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,15 @@ public <T> T deserialize(HttpResponse response, TypeReference<T> valueType) thro
703703
String mimeType = getResponseMimeType(response);
704704
if (mimeType == null || isJsonMime(mimeType)) {
705705
// Assume json if no mime type
706-
return objectMapper.readValue(entity.getContent(), valueType);
706+
// convert input stream to string
707+
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
708+
String content = (String) (s.hasNext() ? s.next() : "");
709+
710+
if ("".equals(content)) { // returns null for empty body
711+
return null;
712+
}
713+
714+
return objectMapper.readValue(content, valueType);
707715
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
708716
// convert input stream to string
709717
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
@@ -942,7 +950,7 @@ public <T> T invokeAPI(
942950
}
943951
} else {
944952
// for empty body
945-
builder.setEntity(serialize(null, null, contentTypeObj));
953+
builder.setEntity(new StringEntity("", contentTypeObj));
946954
}
947955

948956
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {

samples/client/petstore/java/apache-httpclient/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,15 @@ public <T> T deserialize(HttpResponse response, TypeReference<T> valueType) thro
846846
String mimeType = getResponseMimeType(response);
847847
if (mimeType == null || isJsonMime(mimeType)) {
848848
// Assume json if no mime type
849-
return objectMapper.readValue(entity.getContent(), valueType);
849+
// convert input stream to string
850+
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
851+
String content = (String) (s.hasNext() ? s.next() : "");
852+
853+
if ("".equals(content)) { // returns null for empty body
854+
return null;
855+
}
856+
857+
return objectMapper.readValue(content, valueType);
850858
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
851859
// convert input stream to string
852860
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
@@ -1085,7 +1093,7 @@ public <T> T invokeAPI(
10851093
}
10861094
} else {
10871095
// for empty body
1088-
builder.setEntity(serialize(null, null, contentTypeObj));
1096+
builder.setEntity(new StringEntity("", contentTypeObj));
10891097
}
10901098

10911099
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {

0 commit comments

Comments
 (0)