diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java index a881614a0ff..6d9966c7fe4 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java @@ -109,9 +109,10 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator); supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("ApiClient.mustache", invokerFolder, "ApiClient.java")); - supportingFiles.add(new SupportingFile("JsonUtil.mustache", invokerFolder, "JsonUtil.java")); supportingFiles.add(new SupportingFile("apiException.mustache", invokerFolder, "ApiException.java")); supportingFiles.add(new SupportingFile("Configuration.mustache", invokerFolder, "Configuration.java")); + supportingFiles.add(new SupportingFile("JsonUtil.mustache", invokerFolder, "JsonUtil.java")); + supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java")); final String authFolder = (sourceFolder + File.separator + invokerPackage + ".auth").replace(".", File.separator); supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java")); diff --git a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache index 910728c4dd5..352401ba0d8 100644 --- a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache @@ -238,6 +238,36 @@ public class ApiClient { } } + /** + * Select the Accept header's value from the given accepts array: + * if JSON exists in the given array, use it; + * otherwise use all of them (joining into a string) + * + * @param accepts The accepts array to select from + * @return The Accept header to use. If the given array is empty, + * null will be returned (not to set the Accept header explicitly). + */ + public String selectHeaderAccept(String[] accepts) { + if (accepts.length == 0) return null; + if (StringUtil.containsIgnoreCase(accepts, "application/json")) return "application/json"; + return StringUtil.join(accepts, ","); + } + + /** + * Select the Content-Type header's value from the given array: + * if JSON exists in the given array, use it; + * otherwise use the first one of the array. + * + * @param contentTypes The Content-Type array to select from + * @return The Content-Type header to use. If the given array is empty, + * JSON will be used. + */ + public String selectHeaderContentType(String[] contentTypes) { + if (contentTypes.length == 0) return "application/json"; + if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json"; + return contentTypes[0]; + } + /** * Escape the given string to be used as URL query value. */ @@ -306,11 +336,12 @@ public class ApiClient { * @param body The request body object * @param headerParams The header parameters * @param formParams The form parameters - * @param contentType The request Content-Type + * @param accept The request's Accept header + * @param contentType The request's Content-Type header * @param authNames The authentications to apply * @return The response body in type of string */ - public String invokeAPI(String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String contentType, String[] authNames) throws ApiException { + public String invokeAPI(String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String accept, String contentType, String[] authNames) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams); Client client = getClient(); @@ -328,16 +359,21 @@ public class ApiClient { } String querystring = b.toString(); - Builder builder = client.resource(basePath + path + querystring).accept("application/json"); + Builder builder; + if (accept == null) + builder = client.resource(basePath + path + querystring).getRequestBuilder(); + else + builder = client.resource(basePath + path + querystring).accept(accept); + for(String key : headerParams.keySet()) { builder = builder.header(key, headerParams.get(key)); } - for(String key : defaultHeaderMap.keySet()) { if(!headerParams.containsKey(key)) { builder = builder.header(key, defaultHeaderMap.get(key)); } } + ClientResponse response = null; if("GET".equals(method)) { diff --git a/modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache b/modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache new file mode 100644 index 00000000000..035d6739dce --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache @@ -0,0 +1,41 @@ +package {{invokerPackage}}; + +public class StringUtil { + /** + * Check if the given array contains the given value (with case-insensitive comparison). + * + * @param array The array + * @param value The value to search + * @return true if the array contains the value + */ + public static boolean containsIgnoreCase(String[] array, String value) { + for (String str : array) { + if (value == null && str == null) return true; + if (value != null && value.equalsIgnoreCase(str)) return true; + } + return false; + } + + /** + * Join an array of strings with the given separator. + *

+ * Note: This might be replaced by utility method from commons-lang or guava someday + * if one of those libraries is added as dependency. + *

+ * + * @param array The array of strings + * @param separator The separator + * @return the resulting string + */ + public static String join(String[] array, String separator) { + int len = array.length; + if (len == 0) return ""; + + StringBuilder out = new StringBuilder(); + out.append(array[0]); + for (int i = 1; i < len; i++) { + out.append(separator).append(array[i]); + } + return out.toString(); + } +} diff --git a/modules/swagger-codegen/src/main/resources/Java/api.mustache b/modules/swagger-codegen/src/main/resources/Java/api.mustache index 1e2058e3a02..63357afd896 100644 --- a/modules/swagger-codegen/src/main/resources/Java/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/api.mustache @@ -68,14 +68,20 @@ public class {{classname}} { {{#queryParams}}if ({{paramName}} != null) queryParams.put("{{baseName}}", apiClient.parameterToString({{paramName}})); {{/queryParams}} + {{#headerParams}}if ({{paramName}} != null) headerParams.put("{{baseName}}", apiClient.parameterToString({{paramName}})); {{/headerParams}} - String[] contentTypes = { - {{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}} - }; - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String[] accepts = { + {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { + {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} + }; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -103,7 +109,7 @@ public class {{classname}} { try { String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} }; - String response = apiClient.invokeAPI(path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return {{#returnType}}({{{returnType}}}) apiClient.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}}; } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java index 61d296537e6..0adcbe7031d 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java @@ -237,6 +237,36 @@ public class ApiClient { } } + /** + * Select the Accept header's value from the given accepts array: + * if JSON exists in the given array, use it; + * otherwise use all of them (joining into a string) + * + * @param accepts The accepts array to select from + * @return The Accept header to use. If the given array is empty, + * null will be returned (not to set the Accept header explicitly). + */ + public String selectHeaderAccept(String[] accepts) { + if (accepts.length == 0) return null; + if (StringUtil.containsIgnoreCase(accepts, "application/json")) return "application/json"; + return StringUtil.join(accepts, ","); + } + + /** + * Select the Content-Type header's value from the given array: + * if JSON exists in the given array, use it; + * otherwise use the first one of the array. + * + * @param contentTypes The Content-Type array to select from + * @return The Content-Type header to use. If the given array is empty, + * JSON will be used. + */ + public String selectHeaderContentType(String[] contentTypes) { + if (contentTypes.length == 0) return "application/json"; + if (StringUtil.containsIgnoreCase(contentTypes, "application/json")) return "application/json"; + return contentTypes[0]; + } + /** * Escape the given string to be used as URL query value. */ @@ -305,11 +335,12 @@ public class ApiClient { * @param body The request body object * @param headerParams The header parameters * @param formParams The form parameters - * @param contentType The request Content-Type + * @param accept The request's Accept header + * @param contentType The request's Content-Type header * @param authNames The authentications to apply * @return The response body in type of string */ - public String invokeAPI(String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String contentType, String[] authNames) throws ApiException { + public String invokeAPI(String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String accept, String contentType, String[] authNames) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams); Client client = getClient(); @@ -327,16 +358,21 @@ public class ApiClient { } String querystring = b.toString(); - Builder builder = client.resource(basePath + path + querystring).accept("application/json"); + Builder builder; + if (accept == null) + builder = client.resource(basePath + path + querystring).getRequestBuilder(); + else + builder = client.resource(basePath + path + querystring).accept(accept); + for(String key : headerParams.keySet()) { builder = builder.header(key, headerParams.get(key)); } - for(String key : defaultHeaderMap.keySet()) { if(!headerParams.containsKey(key)) { builder = builder.header(key, defaultHeaderMap.get(key)); } } + ClientResponse response = null; if("GET".equals(method)) { diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/StringUtil.java b/samples/client/petstore/java/src/main/java/io/swagger/client/StringUtil.java new file mode 100644 index 00000000000..5b5af2d5ce0 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/StringUtil.java @@ -0,0 +1,41 @@ +package io.swagger.client; + +public class StringUtil { + /** + * Check if the given array contains the given value (with case-insensitive comparison). + * + * @param array The array + * @param value The value to search + * @return true if the array contains the value + */ + public static boolean containsIgnoreCase(String[] array, String value) { + for (String str : array) { + if (value == null && str == null) return true; + if (value != null && value.equalsIgnoreCase(str)) return true; + } + return false; + } + + /** + * Join an array of strings with the given separator. + *

+ * Note: This might be replaced by utility method from commons-lang or guava someday + * if one of those libraries is added as dependency. + *

+ * + * @param array The array of strings + * @param separator The separator + * @return the resulting string + */ + public static String join(String[] array, String separator) { + int len = array.length; + if (len == 0) return ""; + + StringBuilder out = new StringBuilder(); + out.append(array[0]); + for (int i = 1; i < len; i++) { + out.append(separator).append(array[i]); + } + return out.toString(); + } +} diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java b/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java index f8dd9a08553..bc793fd41ee 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java @@ -59,12 +59,18 @@ public class PetApi { Map formParams = new HashMap(); - - String[] contentTypes = { - "application/json","application/xml" - }; - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { + "application/json", "application/xml" + }; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -79,7 +85,7 @@ public class PetApi { try { String[] authNames = new String[] { "petstore_auth" }; - String response = apiClient.invokeAPI(path, "PUT", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "PUT", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -110,12 +116,18 @@ public class PetApi { Map formParams = new HashMap(); - - String[] contentTypes = { - "application/json","application/xml" - }; - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { + "application/json", "application/xml" + }; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -130,7 +142,7 @@ public class PetApi { try { String[] authNames = new String[] { "petstore_auth" }; - String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -163,12 +175,18 @@ public class PetApi { if (status != null) queryParams.put("status", apiClient.parameterToString(status)); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -183,7 +201,7 @@ public class PetApi { try { String[] authNames = new String[] { "petstore_auth" }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (List) apiClient.deserialize(response, "array", Pet.class); } @@ -216,12 +234,18 @@ public class PetApi { if (tags != null) queryParams.put("tags", apiClient.parameterToString(tags)); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -236,7 +260,7 @@ public class PetApi { try { String[] authNames = new String[] { "petstore_auth" }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (List) apiClient.deserialize(response, "array", Pet.class); } @@ -273,12 +297,18 @@ public class PetApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -293,7 +323,7 @@ public class PetApi { try { String[] authNames = new String[] { "api_key", "petstore_auth" }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (Pet) apiClient.deserialize(response, "", Pet.class); } @@ -332,12 +362,18 @@ public class PetApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { "application/x-www-form-urlencoded" }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -366,7 +402,7 @@ public class PetApi { try { String[] authNames = new String[] { "petstore_auth" }; - String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -404,14 +440,20 @@ public class PetApi { Map formParams = new HashMap(); + if (apiKey != null) headerParams.put("api_key", apiClient.parameterToString(apiKey)); - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -426,7 +468,7 @@ public class PetApi { try { String[] authNames = new String[] { "petstore_auth" }; - String response = apiClient.invokeAPI(path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "DELETE", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -465,12 +507,18 @@ public class PetApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { "multipart/form-data" }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -499,7 +547,7 @@ public class PetApi { try { String[] authNames = new String[] { "petstore_auth" }; - String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java b/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java index 7af691b2aa6..727e791c603 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java @@ -58,12 +58,18 @@ public class StoreApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -78,7 +84,7 @@ public class StoreApi { try { String[] authNames = new String[] { "api_key" }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (Map) apiClient.deserialize(response, "map", Map.class); } @@ -109,12 +115,18 @@ public class StoreApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -129,7 +141,7 @@ public class StoreApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (Order) apiClient.deserialize(response, "", Order.class); } @@ -166,12 +178,18 @@ public class StoreApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -186,7 +204,7 @@ public class StoreApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (Order) apiClient.deserialize(response, "", Order.class); } @@ -223,12 +241,18 @@ public class StoreApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -243,7 +267,7 @@ public class StoreApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "DELETE", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java b/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java index ffa9235d271..9e55bbed590 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java @@ -59,12 +59,18 @@ public class UserApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -79,7 +85,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -110,12 +116,18 @@ public class UserApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -130,7 +142,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -161,12 +173,18 @@ public class UserApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -181,7 +199,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "POST", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -217,12 +235,18 @@ public class UserApi { if (password != null) queryParams.put("password", apiClient.parameterToString(password)); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -237,7 +261,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (String) apiClient.deserialize(response, "", String.class); } @@ -267,12 +291,18 @@ public class UserApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -287,7 +317,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -324,12 +354,18 @@ public class UserApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -344,7 +380,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "GET", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return (User) apiClient.deserialize(response, "", User.class); } @@ -382,12 +418,18 @@ public class UserApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -402,7 +444,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "PUT", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "PUT", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } @@ -439,12 +481,18 @@ public class UserApi { Map formParams = new HashMap(); + - String[] contentTypes = { + + final String[] accepts = { + "application/json", "application/xml" + }; + final String accept = apiClient.selectHeaderAccept(accepts); + + final String[] contentTypes = { }; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + final String contentType = apiClient.selectHeaderContentType(contentTypes); if(contentType.startsWith("multipart/form-data")) { boolean hasFields = false; @@ -459,7 +507,7 @@ public class UserApi { try { String[] authNames = new String[] { }; - String response = apiClient.invokeAPI(path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames); + String response = apiClient.invokeAPI(path, "DELETE", queryParams, postBody, headerParams, formParams, accept, contentType, authNames); if(response != null){ return ; } diff --git a/samples/client/petstore/java/src/test/java/io/swagger/client/ApiClientTest.java b/samples/client/petstore/java/src/test/java/io/swagger/client/ApiClientTest.java index 82f39e90583..d0ec118083a 100644 --- a/samples/client/petstore/java/src/test/java/io/swagger/client/ApiClientTest.java +++ b/samples/client/petstore/java/src/test/java/io/swagger/client/ApiClientTest.java @@ -15,6 +15,42 @@ public class ApiClientTest { apiClient = new ApiClient(); } + @Test + public void testSelectHeaderAccept() { + String[] accepts = { "APPLICATION/JSON", "APPLICATION/XML" }; + assertEquals("application/json", apiClient.selectHeaderAccept(accepts)); + + accepts = new String[] { "application/json", "application/xml" }; + assertEquals("application/json", apiClient.selectHeaderAccept(accepts)); + + accepts = new String[] { "application/xml", "application/json" }; + assertEquals("application/json", apiClient.selectHeaderAccept(accepts)); + + accepts = new String[] { "text/plain", "application/xml" }; + assertEquals("text/plain,application/xml", apiClient.selectHeaderAccept(accepts)); + + accepts = new String[] { }; + assertNull(apiClient.selectHeaderAccept(accepts)); + } + + @Test + public void testSelectHeaderContentType() { + String[] contentTypes = { "APPLICATION/JSON", "APPLICATION/XML" }; + assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); + + contentTypes = new String[] { "application/json", "application/xml" }; + assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); + + contentTypes = new String[] { "application/xml", "application/json" }; + assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); + + contentTypes = new String[] { "text/plain", "application/xml" }; + assertEquals("text/plain", apiClient.selectHeaderContentType(contentTypes)); + + contentTypes = new String[] { }; + assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); + } + @Test public void testGetAuthentications() { Map auths = apiClient.getAuthentications(); diff --git a/samples/client/petstore/java/src/test/java/io/swagger/client/StringUtilTest.java b/samples/client/petstore/java/src/test/java/io/swagger/client/StringUtilTest.java new file mode 100644 index 00000000000..a9abfb03290 --- /dev/null +++ b/samples/client/petstore/java/src/test/java/io/swagger/client/StringUtilTest.java @@ -0,0 +1,32 @@ +package io.swagger.client; + +import static org.junit.Assert.*; +import org.junit.*; + +public class StringUtilTest { + @Test + public void testContainsIgnoreCase() { + assertTrue(StringUtil.containsIgnoreCase(new String[]{ "abc" }, "abc")); + assertTrue(StringUtil.containsIgnoreCase(new String[]{ "abc" }, "ABC")); + assertTrue(StringUtil.containsIgnoreCase(new String[]{ "ABC" }, "abc")); + assertTrue(StringUtil.containsIgnoreCase(new String[]{ null, "abc" }, "ABC")); + assertTrue(StringUtil.containsIgnoreCase(new String[]{ null, "abc" }, null)); + + assertFalse(StringUtil.containsIgnoreCase(new String[]{ "abc" }, "def")); + assertFalse(StringUtil.containsIgnoreCase(new String[]{ }, "ABC")); + assertFalse(StringUtil.containsIgnoreCase(new String[]{ }, null)); + } + + @Test + public void testJoin() { + String[] array = { "aa", "bb", "cc" }; + assertEquals("aa,bb,cc", StringUtil.join(array, ",")); + assertEquals("aa, bb, cc", StringUtil.join(array, ", ")); + assertEquals("aabbcc", StringUtil.join(array, "")); + assertEquals("aa bb cc", StringUtil.join(array, " ")); + assertEquals("aa\nbb\ncc", StringUtil.join(array, "\n")); + + assertEquals("", StringUtil.join(new String[]{ }, ",")); + assertEquals("abc", StringUtil.join(new String[]{ "abc" }, ",")); + } +}