Skip to content

Commit 7ccd039

Browse files
authored
[java][okhttp-gson] fix: JSON deserialization fallback for String return types (#22498)
* Use String-based JSON deserialize method with fallback for String return types * Regenerate samples
1 parent 8f8001e commit 7ccd039

File tree

16 files changed

+176
-16
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson
  • samples/client
    • echo_api/java
      • okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client
      • okhttp-gson/src/main/java/org/openapitools/client
    • others/java
      • okhttp-gson-oneOf-array/src/main/java/org/openapitools/client
      • okhttp-gson-oneOf/src/main/java/org/openapitools/client
      • okhttp-gson-streaming/src/main/java/org/openapitools/client
    • petstore/java
      • okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client
      • okhttp-gson-3.1/src/main/java/org/openapitools/client
      • okhttp-gson-awsv4signature/src/main/java/org/openapitools/client
      • okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client
      • okhttp-gson-group-parameter/src/main/java/org/openapitools/client
      • okhttp-gson-nullable-required/src/main/java/org/openapitools/client
      • okhttp-gson-parcelableModel/src/main/java/org/openapitools/client
      • okhttp-gson-swagger1/src/main/java/org/openapitools/client
      • okhttp-gson-swagger2/src/main/java/org/openapitools/client
      • okhttp-gson/src/main/java/org/openapitools/client

16 files changed

+176
-16
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,17 @@ public class ApiClient {
12021202
}
12031203
try {
12041204
if (isJsonMime(contentType)) {
1205-
return JSON.deserialize(respBody.byteStream(), returnType);
1205+
if (returnType.equals(String.class)) {
1206+
String respBodyString = respBody.string();
1207+
if (respBodyString.isEmpty()) {
1208+
return null;
1209+
}
1210+
// Use String-based deserialize for String return type with fallback
1211+
return JSON.deserialize(respBodyString, returnType);
1212+
} else {
1213+
// Use InputStream-based deserialize which supports responses > 2GB
1214+
return JSON.deserialize(respBody.byteStream(), returnType);
1215+
}
12061216
} else if (returnType.equals(String.class)) {
12071217
String respBodyString = respBody.string();
12081218
if (respBodyString.isEmpty()) {

samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10421042
}
10431043
try {
10441044
if (isJsonMime(contentType)) {
1045-
return JSON.deserialize(respBody.byteStream(), returnType);
1045+
if (returnType.equals(String.class)) {
1046+
String respBodyString = respBody.string();
1047+
if (respBodyString.isEmpty()) {
1048+
return null;
1049+
}
1050+
// Use String-based deserialize for String return type with fallback
1051+
return JSON.deserialize(respBodyString, returnType);
1052+
} else {
1053+
// Use InputStream-based deserialize which supports responses > 2GB
1054+
return JSON.deserialize(respBody.byteStream(), returnType);
1055+
}
10461056
} else if (returnType.equals(String.class)) {
10471057
String respBodyString = respBody.string();
10481058
if (respBodyString.isEmpty()) {

samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
972972
}
973973
try {
974974
if (isJsonMime(contentType)) {
975-
return JSON.deserialize(respBody.byteStream(), returnType);
975+
if (returnType.equals(String.class)) {
976+
String respBodyString = respBody.string();
977+
if (respBodyString.isEmpty()) {
978+
return null;
979+
}
980+
// Use String-based deserialize for String return type with fallback
981+
return JSON.deserialize(respBodyString, returnType);
982+
} else {
983+
// Use InputStream-based deserialize which supports responses > 2GB
984+
return JSON.deserialize(respBody.byteStream(), returnType);
985+
}
976986
} else if (returnType.equals(String.class)) {
977987
String respBodyString = respBody.string();
978988
if (respBodyString.isEmpty()) {

samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
946946
}
947947
try {
948948
if (isJsonMime(contentType)) {
949-
return JSON.deserialize(respBody.byteStream(), returnType);
949+
if (returnType.equals(String.class)) {
950+
String respBodyString = respBody.string();
951+
if (respBodyString.isEmpty()) {
952+
return null;
953+
}
954+
// Use String-based deserialize for String return type with fallback
955+
return JSON.deserialize(respBodyString, returnType);
956+
} else {
957+
// Use InputStream-based deserialize which supports responses > 2GB
958+
return JSON.deserialize(respBody.byteStream(), returnType);
959+
}
950960
} else if (returnType.equals(String.class)) {
951961
String respBodyString = respBody.string();
952962
if (respBodyString.isEmpty()) {

samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
946946
}
947947
try {
948948
if (isJsonMime(contentType)) {
949-
return JSON.deserialize(respBody.byteStream(), returnType);
949+
if (returnType.equals(String.class)) {
950+
String respBodyString = respBody.string();
951+
if (respBodyString.isEmpty()) {
952+
return null;
953+
}
954+
// Use String-based deserialize for String return type with fallback
955+
return JSON.deserialize(respBodyString, returnType);
956+
} else {
957+
// Use InputStream-based deserialize which supports responses > 2GB
958+
return JSON.deserialize(respBody.byteStream(), returnType);
959+
}
950960
} else if (returnType.equals(String.class)) {
951961
String respBodyString = respBody.string();
952962
if (respBodyString.isEmpty()) {

samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
946946
}
947947
try {
948948
if (isJsonMime(contentType)) {
949-
return JSON.deserialize(respBody.byteStream(), returnType);
949+
if (returnType.equals(String.class)) {
950+
String respBodyString = respBody.string();
951+
if (respBodyString.isEmpty()) {
952+
return null;
953+
}
954+
// Use String-based deserialize for String return type with fallback
955+
return JSON.deserialize(respBodyString, returnType);
956+
} else {
957+
// Use InputStream-based deserialize which supports responses > 2GB
958+
return JSON.deserialize(respBody.byteStream(), returnType);
959+
}
950960
} else if (returnType.equals(String.class)) {
951961
String respBodyString = respBody.string();
952962
if (respBodyString.isEmpty()) {

samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10421042
}
10431043
try {
10441044
if (isJsonMime(contentType)) {
1045-
return JSON.deserialize(respBody.byteStream(), returnType);
1045+
if (returnType.equals(String.class)) {
1046+
String respBodyString = respBody.string();
1047+
if (respBodyString.isEmpty()) {
1048+
return null;
1049+
}
1050+
// Use String-based deserialize for String return type with fallback
1051+
return JSON.deserialize(respBodyString, returnType);
1052+
} else {
1053+
// Use InputStream-based deserialize which supports responses > 2GB
1054+
return JSON.deserialize(respBody.byteStream(), returnType);
1055+
}
10461056
} else if (returnType.equals(String.class)) {
10471057
String respBodyString = respBody.string();
10481058
if (respBodyString.isEmpty()) {

samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10421042
}
10431043
try {
10441044
if (isJsonMime(contentType)) {
1045-
return JSON.deserialize(respBody.byteStream(), returnType);
1045+
if (returnType.equals(String.class)) {
1046+
String respBodyString = respBody.string();
1047+
if (respBodyString.isEmpty()) {
1048+
return null;
1049+
}
1050+
// Use String-based deserialize for String return type with fallback
1051+
return JSON.deserialize(respBodyString, returnType);
1052+
} else {
1053+
// Use InputStream-based deserialize which supports responses > 2GB
1054+
return JSON.deserialize(respBody.byteStream(), returnType);
1055+
}
10461056
} else if (returnType.equals(String.class)) {
10471057
String respBodyString = respBody.string();
10481058
if (respBodyString.isEmpty()) {

samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10621062
}
10631063
try {
10641064
if (isJsonMime(contentType)) {
1065-
return JSON.deserialize(respBody.byteStream(), returnType);
1065+
if (returnType.equals(String.class)) {
1066+
String respBodyString = respBody.string();
1067+
if (respBodyString.isEmpty()) {
1068+
return null;
1069+
}
1070+
// Use String-based deserialize for String return type with fallback
1071+
return JSON.deserialize(respBodyString, returnType);
1072+
} else {
1073+
// Use InputStream-based deserialize which supports responses > 2GB
1074+
return JSON.deserialize(respBody.byteStream(), returnType);
1075+
}
10661076
} else if (returnType.equals(String.class)) {
10671077
String respBodyString = respBody.string();
10681078
if (respBodyString.isEmpty()) {

samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10471047
}
10481048
try {
10491049
if (isJsonMime(contentType)) {
1050-
return JSON.deserialize(respBody.byteStream(), returnType);
1050+
if (returnType.equals(String.class)) {
1051+
String respBodyString = respBody.string();
1052+
if (respBodyString.isEmpty()) {
1053+
return null;
1054+
}
1055+
// Use String-based deserialize for String return type with fallback
1056+
return JSON.deserialize(respBodyString, returnType);
1057+
} else {
1058+
// Use InputStream-based deserialize which supports responses > 2GB
1059+
return JSON.deserialize(respBody.byteStream(), returnType);
1060+
}
10511061
} else if (returnType.equals(String.class)) {
10521062
String respBodyString = respBody.string();
10531063
if (respBodyString.isEmpty()) {

0 commit comments

Comments
 (0)