Merge pull request #790 from xhh/java-accept-contenttype

Minor improvement to Java API client accept and content-type header
This commit is contained in:
Tony Tam 2015-06-05 17:51:23 -07:00
commit c3d2a581c9
11 changed files with 447 additions and 98 deletions

View File

@ -109,9 +109,10 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator); final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator);
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", invokerFolder, "ApiClient.java")); 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("apiException.mustache", invokerFolder, "ApiException.java"));
supportingFiles.add(new SupportingFile("Configuration.mustache", invokerFolder, "Configuration.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); final String authFolder = (sourceFolder + File.separator + invokerPackage + ".auth").replace(".", File.separator);
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java")); supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));

View File

@ -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. * 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 body The request body object
* @param headerParams The header parameters * @param headerParams The header parameters
* @param formParams The form 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 * @param authNames The authentications to apply
* @return The response body in type of string * @return The response body in type of string
*/ */
public String invokeAPI(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException { public String invokeAPI(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String accept, String contentType, String[] authNames) throws ApiException {
updateParamsForAuth(authNames, queryParams, headerParams); updateParamsForAuth(authNames, queryParams, headerParams);
Client client = getClient(); Client client = getClient();
@ -328,16 +359,21 @@ public class ApiClient {
} }
String querystring = b.toString(); 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()) { for(String key : headerParams.keySet()) {
builder = builder.header(key, headerParams.get(key)); builder = builder.header(key, headerParams.get(key));
} }
for(String key : defaultHeaderMap.keySet()) { for(String key : defaultHeaderMap.keySet()) {
if(!headerParams.containsKey(key)) { if(!headerParams.containsKey(key)) {
builder = builder.header(key, defaultHeaderMap.get(key)); builder = builder.header(key, defaultHeaderMap.get(key));
} }
} }
ClientResponse response = null; ClientResponse response = null;
if("GET".equals(method)) { if("GET".equals(method)) {

View File

@ -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.
* <p>
* Note: This might be replaced by utility method from commons-lang or guava someday
* if one of those libraries is added as dependency.
* </p>
*
* @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();
}
}

View File

@ -68,14 +68,20 @@ public class {{classname}} {
{{#queryParams}}if ({{paramName}} != null) {{#queryParams}}if ({{paramName}} != null)
queryParams.put("{{baseName}}", apiClient.parameterToString({{paramName}})); queryParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
{{/queryParams}} {{/queryParams}}
{{#headerParams}}if ({{paramName}} != null) {{#headerParams}}if ({{paramName}} != null)
headerParams.put("{{baseName}}", apiClient.parameterToString({{paramName}})); headerParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
{{/headerParams}} {{/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")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -103,7 +109,7 @@ public class {{classname}} {
try { try {
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} }; 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){ if(response != null){
return {{#returnType}}({{{returnType}}}) apiClient.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}}; return {{#returnType}}({{{returnType}}}) apiClient.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
} }

View File

@ -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. * 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 body The request body object
* @param headerParams The header parameters * @param headerParams The header parameters
* @param formParams The form 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 * @param authNames The authentications to apply
* @return The response body in type of string * @return The response body in type of string
*/ */
public String invokeAPI(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException { public String invokeAPI(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String accept, String contentType, String[] authNames) throws ApiException {
updateParamsForAuth(authNames, queryParams, headerParams); updateParamsForAuth(authNames, queryParams, headerParams);
Client client = getClient(); Client client = getClient();
@ -327,16 +358,21 @@ public class ApiClient {
} }
String querystring = b.toString(); 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()) { for(String key : headerParams.keySet()) {
builder = builder.header(key, headerParams.get(key)); builder = builder.header(key, headerParams.get(key));
} }
for(String key : defaultHeaderMap.keySet()) { for(String key : defaultHeaderMap.keySet()) {
if(!headerParams.containsKey(key)) { if(!headerParams.containsKey(key)) {
builder = builder.header(key, defaultHeaderMap.get(key)); builder = builder.header(key, defaultHeaderMap.get(key));
} }
} }
ClientResponse response = null; ClientResponse response = null;
if("GET".equals(method)) { if("GET".equals(method)) {

View File

@ -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.
* <p>
* Note: This might be replaced by utility method from commons-lang or guava someday
* if one of those libraries is added as dependency.
* </p>
*
* @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();
}
}

View File

@ -60,11 +60,17 @@ public class PetApi {
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")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -79,7 +85,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "petstore_auth" }; 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){ if(response != null){
return ; return ;
} }
@ -111,11 +117,17 @@ public class PetApi {
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")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -130,7 +142,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "petstore_auth" }; 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){ if(response != null){
return ; return ;
} }
@ -164,11 +176,17 @@ public class PetApi {
queryParams.put("status", apiClient.parameterToString(status)); queryParams.put("status", apiClient.parameterToString(status));
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -183,7 +201,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "petstore_auth" }; 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){ if(response != null){
return (List<Pet>) apiClient.deserialize(response, "array", Pet.class); return (List<Pet>) apiClient.deserialize(response, "array", Pet.class);
} }
@ -217,11 +235,17 @@ public class PetApi {
queryParams.put("tags", apiClient.parameterToString(tags)); queryParams.put("tags", apiClient.parameterToString(tags));
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -236,7 +260,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "petstore_auth" }; 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){ if(response != null){
return (List<Pet>) apiClient.deserialize(response, "array", Pet.class); return (List<Pet>) apiClient.deserialize(response, "array", Pet.class);
} }
@ -274,11 +298,17 @@ public class PetApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -293,7 +323,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "api_key", "petstore_auth" }; 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){ if(response != null){
return (Pet) apiClient.deserialize(response, "", Pet.class); return (Pet) apiClient.deserialize(response, "", Pet.class);
} }
@ -333,11 +363,17 @@ public class PetApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
"application/x-www-form-urlencoded" "application/x-www-form-urlencoded"
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -366,7 +402,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "petstore_auth" }; 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){ if(response != null){
return ; return ;
} }
@ -404,14 +440,20 @@ public class PetApi {
Map<String, String> formParams = new HashMap<String, String>(); Map<String, String> formParams = new HashMap<String, String>();
if (apiKey != null) if (apiKey != null)
headerParams.put("api_key", apiClient.parameterToString(apiKey)); 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 = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -426,7 +468,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "petstore_auth" }; 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){ if(response != null){
return ; return ;
} }
@ -466,11 +508,17 @@ public class PetApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
"multipart/form-data" "multipart/form-data"
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -499,7 +547,7 @@ public class PetApi {
try { try {
String[] authNames = new String[] { "petstore_auth" }; 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){ if(response != null){
return ; return ;
} }

View File

@ -59,11 +59,17 @@ public class StoreApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -78,7 +84,7 @@ public class StoreApi {
try { try {
String[] authNames = new String[] { "api_key" }; 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){ if(response != null){
return (Map<String, Integer>) apiClient.deserialize(response, "map", Map.class); return (Map<String, Integer>) apiClient.deserialize(response, "map", Map.class);
} }
@ -110,11 +116,17 @@ public class StoreApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -129,7 +141,7 @@ public class StoreApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return (Order) apiClient.deserialize(response, "", Order.class); return (Order) apiClient.deserialize(response, "", Order.class);
} }
@ -167,11 +179,17 @@ public class StoreApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -186,7 +204,7 @@ public class StoreApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return (Order) apiClient.deserialize(response, "", Order.class); return (Order) apiClient.deserialize(response, "", Order.class);
} }
@ -224,11 +242,17 @@ public class StoreApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -243,7 +267,7 @@ public class StoreApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return ; return ;
} }

View File

@ -60,11 +60,17 @@ public class UserApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -79,7 +85,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return ; return ;
} }
@ -111,11 +117,17 @@ public class UserApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -130,7 +142,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return ; return ;
} }
@ -162,11 +174,17 @@ public class UserApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -181,7 +199,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return ; return ;
} }
@ -218,11 +236,17 @@ public class UserApi {
queryParams.put("password", apiClient.parameterToString(password)); queryParams.put("password", apiClient.parameterToString(password));
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -237,7 +261,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return (String) apiClient.deserialize(response, "", String.class); return (String) apiClient.deserialize(response, "", String.class);
} }
@ -268,11 +292,17 @@ public class UserApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -287,7 +317,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return ; return ;
} }
@ -325,11 +355,17 @@ public class UserApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -344,7 +380,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return (User) apiClient.deserialize(response, "", User.class); return (User) apiClient.deserialize(response, "", User.class);
} }
@ -383,11 +419,17 @@ public class UserApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -402,7 +444,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return ; return ;
} }
@ -440,11 +482,17 @@ public class UserApi {
String[] contentTypes = {
final String[] accepts = {
"application/json", "application/xml"
};
final String accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
}; };
final String contentType = apiClient.selectHeaderContentType(contentTypes);
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("multipart/form-data")) { if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false; boolean hasFields = false;
@ -459,7 +507,7 @@ public class UserApi {
try { try {
String[] authNames = new String[] { }; 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){ if(response != null){
return ; return ;
} }

View File

@ -15,6 +15,42 @@ public class ApiClientTest {
apiClient = new ApiClient(); 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 @Test
public void testGetAuthentications() { public void testGetAuthentications() {
Map<String, Authentication> auths = apiClient.getAuthentications(); Map<String, Authentication> auths = apiClient.getAuthentications();

View File

@ -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" }, ","));
}
}