Skip to content

Commit 7f2a27a

Browse files
committed
Use String-based JSON deserialize method with fallback for String return types
1 parent 4f9f14a commit 7f2a27a

File tree

16 files changed

+116
-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

+116
-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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,13 @@ 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+
// Use String-based deserialize for String return type with fallback
1047+
return JSON.deserialize(respBody.string(), returnType);
1048+
} else {
1049+
// Use InputStream-based deserialize which supports responses > 2GB
1050+
return JSON.deserialize(respBody.byteStream(), returnType);
1051+
}
10461052
} else if (returnType.equals(String.class)) {
10471053
String respBodyString = respBody.string();
10481054
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,13 @@ 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+
// Use String-based deserialize for String return type with fallback
977+
return JSON.deserialize(respBody.string(), returnType);
978+
} else {
979+
// Use InputStream-based deserialize which supports responses > 2GB
980+
return JSON.deserialize(respBody.byteStream(), returnType);
981+
}
976982
} else if (returnType.equals(String.class)) {
977983
String respBodyString = respBody.string();
978984
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,13 @@ 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+
// Use String-based deserialize for String return type with fallback
951+
return JSON.deserialize(respBody.string(), returnType);
952+
} else {
953+
// Use InputStream-based deserialize which supports responses > 2GB
954+
return JSON.deserialize(respBody.byteStream(), returnType);
955+
}
950956
} else if (returnType.equals(String.class)) {
951957
String respBodyString = respBody.string();
952958
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,13 @@ 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+
// Use String-based deserialize for String return type with fallback
951+
return JSON.deserialize(respBody.string(), returnType);
952+
} else {
953+
// Use InputStream-based deserialize which supports responses > 2GB
954+
return JSON.deserialize(respBody.byteStream(), returnType);
955+
}
950956
} else if (returnType.equals(String.class)) {
951957
String respBodyString = respBody.string();
952958
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,13 @@ 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+
// Use String-based deserialize for String return type with fallback
951+
return JSON.deserialize(respBody.string(), returnType);
952+
} else {
953+
// Use InputStream-based deserialize which supports responses > 2GB
954+
return JSON.deserialize(respBody.byteStream(), returnType);
955+
}
950956
} else if (returnType.equals(String.class)) {
951957
String respBodyString = respBody.string();
952958
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,13 @@ 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+
// Use String-based deserialize for String return type with fallback
1047+
return JSON.deserialize(respBody.string(), returnType);
1048+
} else {
1049+
// Use InputStream-based deserialize which supports responses > 2GB
1050+
return JSON.deserialize(respBody.byteStream(), returnType);
1051+
}
10461052
} else if (returnType.equals(String.class)) {
10471053
String respBodyString = respBody.string();
10481054
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,13 @@ 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+
// Use String-based deserialize for String return type with fallback
1047+
return JSON.deserialize(respBody.string(), returnType);
1048+
} else {
1049+
// Use InputStream-based deserialize which supports responses > 2GB
1050+
return JSON.deserialize(respBody.byteStream(), returnType);
1051+
}
10461052
} else if (returnType.equals(String.class)) {
10471053
String respBodyString = respBody.string();
10481054
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,13 @@ 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+
// Use String-based deserialize for String return type with fallback
1067+
return JSON.deserialize(respBody.string(), returnType);
1068+
} else {
1069+
// Use InputStream-based deserialize which supports responses > 2GB
1070+
return JSON.deserialize(respBody.byteStream(), returnType);
1071+
}
10661072
} else if (returnType.equals(String.class)) {
10671073
String respBodyString = respBody.string();
10681074
if (respBodyString.isEmpty()) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,13 @@ 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+
// Use String-based deserialize for String return type with fallback
1052+
return JSON.deserialize(respBody.string(), returnType);
1053+
} else {
1054+
// Use InputStream-based deserialize which supports responses > 2GB
1055+
return JSON.deserialize(respBody.byteStream(), returnType);
1056+
}
10511057
} else if (returnType.equals(String.class)) {
10521058
String respBodyString = respBody.string();
10531059
if (respBodyString.isEmpty()) {

0 commit comments

Comments
 (0)