From 44bbe301cbd06452b9c0259f507f52fea8b03bd1 Mon Sep 17 00:00:00 2001 From: xhh Date: Wed, 23 Sep 2015 12:49:34 +0800 Subject: [PATCH] Support primitive string response in Java clients --- .../src/main/resources/Java/ApiClient.mustache | 9 ++++++++- .../Java/libraries/jersey2/ApiClient.mustache | 11 +++++++++-- .../Java/libraries/okhttp-gson/ApiClient.mustache | 5 ++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache index 5e18c854cba..339951b2750 100644 --- a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache @@ -385,8 +385,15 @@ public class ApiClient { if (contentType.startsWith("application/json")) { return json.deserialize(body, returnType); + } else if (returnType.getType().equals(String.class)) { + // Expecting string, return the raw response body. + return (T) body; } else { - throw new ApiException(500, "can not deserialize Content-Type: " + contentType); + throw new ApiException( + 500, + "Content type \"" + contentType + "\" is not supported for type: " + + returnType.getType() + ); } } diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 5033c1986f8..6a7189e9d33 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -389,8 +389,15 @@ public class ApiClient { if (contentType.startsWith("application/json")) { return json.deserialize(body, returnType); + } else if (returnType.getType().equals(String.class)) { + // Expecting string, return the raw response body. + return (T) body; } else { - throw new ApiException(500, "can not deserialize Content-Type: " + contentType); + throw new ApiException( + 500, + "Content type \"" + contentType + "\" is not supported for type: " + + returnType.getType() + ); } } @@ -429,7 +436,7 @@ public class ApiClient { } } - Invocation.Builder invocationBuilder = target.request(contentType).accept(accept); + Invocation.Builder invocationBuilder = target.request().accept(accept); for (String key : headerParams.keySet()) { String value = headerParams.get(key); diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache index fcbedb0e9f4..c1a3ea44c37 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache @@ -536,9 +536,12 @@ public class ApiClient { } if (contentType.startsWith("application/json")) { return json.deserialize(respBody, returnType); + } else if (returnType.equals(String.class)) { + // Expecting string, return the raw response body. + return (T) respBody; } else { throw new ApiException( - "Content type \"" + contentType + "\" is not supported", + "Content type \"" + contentType + "\" is not supported for type: " + returnType, response.code(), response.headers().toMultimap(), respBody);