forked from loafle/openapi-generator-original
Merge pull request #517 from xhh/java-param-to-str
Add parameterToString to normalize parameters for Java client
This commit is contained in:
commit
1e1d193556
@ -621,6 +621,8 @@ public class DefaultCodegen {
|
|||||||
count += 1;
|
count += 1;
|
||||||
if (count < operation.getConsumes().size())
|
if (count < operation.getConsumes().size())
|
||||||
mediaType.put("hasMore", "true");
|
mediaType.put("hasMore", "true");
|
||||||
|
else
|
||||||
|
mediaType.put("hasMore", null);
|
||||||
c.add(mediaType);
|
c.add(mediaType);
|
||||||
}
|
}
|
||||||
op.consumes = c;
|
op.consumes = c;
|
||||||
@ -636,6 +638,8 @@ public class DefaultCodegen {
|
|||||||
count += 1;
|
count += 1;
|
||||||
if (count < operation.getProduces().size())
|
if (count < operation.getProduces().size())
|
||||||
mediaType.put("hasMore", "true");
|
mediaType.put("hasMore", "true");
|
||||||
|
else
|
||||||
|
mediaType.put("hasMore", null);
|
||||||
c.add(mediaType);
|
c.add(mediaType);
|
||||||
}
|
}
|
||||||
op.produces = c;
|
op.produces = c;
|
||||||
|
@ -59,9 +59,9 @@ public class {{classname}} {
|
|||||||
Map<String, String> formParams = new HashMap<String, String>();
|
Map<String, String> formParams = new HashMap<String, String>();
|
||||||
|
|
||||||
{{#queryParams}}if ({{paramName}} != null)
|
{{#queryParams}}if ({{paramName}} != null)
|
||||||
queryParams.put("{{baseName}}", String.valueOf({{paramName}}));
|
queryParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
|
||||||
{{/queryParams}}
|
{{/queryParams}}
|
||||||
{{#headerParams}}headerParams.put("{{baseName}}", {{paramName}});
|
{{#headerParams}}headerParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
|
||||||
{{/headerParams}}
|
{{/headerParams}}
|
||||||
String[] contentTypes = {
|
String[] contentTypes = {
|
||||||
{{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}}
|
{{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}}
|
||||||
@ -74,7 +74,7 @@ public class {{classname}} {
|
|||||||
FormDataMultiPart mp = new FormDataMultiPart();
|
FormDataMultiPart mp = new FormDataMultiPart();
|
||||||
{{#formParams}}{{#notFile}}
|
{{#formParams}}{{#notFile}}
|
||||||
hasFields = true;
|
hasFields = true;
|
||||||
mp.field("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE);
|
mp.field("{{baseName}}", ApiInvoker.parameterToString({{paramName}}), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||||
{{/notFile}}{{#isFile}}
|
{{/notFile}}{{#isFile}}
|
||||||
hasFields = true;
|
hasFields = true;
|
||||||
mp.field("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE);
|
mp.field("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||||
@ -83,7 +83,7 @@ public class {{classname}} {
|
|||||||
postBody = mp;
|
postBody = mp;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
{{#formParams}}{{#notFile}}formParams.put("{{baseName}}", {{paramName}});{{/notFile}}
|
{{#formParams}}{{#notFile}}formParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));{{/notFile}}
|
||||||
{{/formParams}}
|
{{/formParams}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,16 +19,75 @@ import javax.ws.rs.core.MediaType;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.io.IOException;
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
|
||||||
public class ApiInvoker {
|
public class ApiInvoker {
|
||||||
private static ApiInvoker INSTANCE = new ApiInvoker();
|
private static ApiInvoker INSTANCE = new ApiInvoker();
|
||||||
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
||||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||||
private boolean isDebug = false;
|
private boolean isDebug = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ISO 8601 date time format.
|
||||||
|
* @see https://en.wikipedia.org/wiki/ISO_8601
|
||||||
|
*/
|
||||||
|
public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ISO 8601 date format.
|
||||||
|
* @see https://en.wikipedia.org/wiki/ISO_8601
|
||||||
|
*/
|
||||||
|
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
static {
|
||||||
|
// Use UTC as the default time zone.
|
||||||
|
DATE_TIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date parseDateTime(String str) {
|
||||||
|
try {
|
||||||
|
return DATE_TIME_FORMAT.parse(str);
|
||||||
|
} catch (java.text.ParseException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date parseDate(String str) {
|
||||||
|
try {
|
||||||
|
return DATE_FORMAT.parse(str);
|
||||||
|
} catch (java.text.ParseException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDateTime(Date datetime) {
|
||||||
|
return DATE_TIME_FORMAT.format(datetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDate(Date date) {
|
||||||
|
return DATE_FORMAT.format(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parameterToString(Object param) {
|
||||||
|
if (param == null) {
|
||||||
|
return "";
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return formatDateTime((Date) param);
|
||||||
|
} else {
|
||||||
|
return String.valueOf(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void enableDebug() {
|
public void enableDebug() {
|
||||||
isDebug = true;
|
isDebug = true;
|
||||||
}
|
}
|
||||||
|
@ -19,16 +19,75 @@ import javax.ws.rs.core.MediaType;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.io.IOException;
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
|
||||||
public class ApiInvoker {
|
public class ApiInvoker {
|
||||||
private static ApiInvoker INSTANCE = new ApiInvoker();
|
private static ApiInvoker INSTANCE = new ApiInvoker();
|
||||||
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
||||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||||
private boolean isDebug = false;
|
private boolean isDebug = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ISO 8601 date time format.
|
||||||
|
* @see https://en.wikipedia.org/wiki/ISO_8601
|
||||||
|
*/
|
||||||
|
public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ISO 8601 date format.
|
||||||
|
* @see https://en.wikipedia.org/wiki/ISO_8601
|
||||||
|
*/
|
||||||
|
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
static {
|
||||||
|
// Use UTC as the default time zone.
|
||||||
|
DATE_TIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date parseDateTime(String str) {
|
||||||
|
try {
|
||||||
|
return DATE_TIME_FORMAT.parse(str);
|
||||||
|
} catch (java.text.ParseException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date parseDate(String str) {
|
||||||
|
try {
|
||||||
|
return DATE_FORMAT.parse(str);
|
||||||
|
} catch (java.text.ParseException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDateTime(Date datetime) {
|
||||||
|
return DATE_TIME_FORMAT.format(datetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDate(Date date) {
|
||||||
|
return DATE_FORMAT.format(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parameterToString(Object param) {
|
||||||
|
if (param == null) {
|
||||||
|
return "";
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return formatDateTime((Date) param);
|
||||||
|
} else {
|
||||||
|
return String.valueOf(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void enableDebug() {
|
public void enableDebug() {
|
||||||
isDebug = true;
|
isDebug = true;
|
||||||
}
|
}
|
||||||
|
@ -148,8 +148,8 @@ public class PetApi {
|
|||||||
Map<String, String> headerParams = new HashMap<String, String>();
|
Map<String, String> headerParams = new HashMap<String, String>();
|
||||||
Map<String, String> formParams = new HashMap<String, String>();
|
Map<String, String> formParams = new HashMap<String, String>();
|
||||||
|
|
||||||
if(!"null".equals(String.valueOf(status)))
|
if (status != null)
|
||||||
queryParams.put("status", String.valueOf(status));
|
queryParams.put("status", ApiInvoker.parameterToString(status));
|
||||||
|
|
||||||
|
|
||||||
String[] contentTypes = {
|
String[] contentTypes = {
|
||||||
@ -200,8 +200,8 @@ public class PetApi {
|
|||||||
Map<String, String> headerParams = new HashMap<String, String>();
|
Map<String, String> headerParams = new HashMap<String, String>();
|
||||||
Map<String, String> formParams = new HashMap<String, String>();
|
Map<String, String> formParams = new HashMap<String, String>();
|
||||||
|
|
||||||
if(!"null".equals(String.valueOf(tags)))
|
if (tags != null)
|
||||||
queryParams.put("tags", String.valueOf(tags));
|
queryParams.put("tags", ApiInvoker.parameterToString(tags));
|
||||||
|
|
||||||
|
|
||||||
String[] contentTypes = {
|
String[] contentTypes = {
|
||||||
@ -317,17 +317,17 @@ public class PetApi {
|
|||||||
FormDataMultiPart mp = new FormDataMultiPart();
|
FormDataMultiPart mp = new FormDataMultiPart();
|
||||||
|
|
||||||
hasFields = true;
|
hasFields = true;
|
||||||
mp.field("name", name, MediaType.MULTIPART_FORM_DATA_TYPE);
|
mp.field("name", ApiInvoker.parameterToString(name), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||||
|
|
||||||
hasFields = true;
|
hasFields = true;
|
||||||
mp.field("status", status, MediaType.MULTIPART_FORM_DATA_TYPE);
|
mp.field("status", ApiInvoker.parameterToString(status), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||||
|
|
||||||
if(hasFields)
|
if(hasFields)
|
||||||
postBody = mp;
|
postBody = mp;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
formParams.put("name", name);
|
formParams.put("name", ApiInvoker.parameterToString(name));
|
||||||
formParams.put("status", status);
|
formParams.put("status", ApiInvoker.parameterToString(status));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,7 +364,7 @@ public class PetApi {
|
|||||||
Map<String, String> formParams = new HashMap<String, String>();
|
Map<String, String> formParams = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
headerParams.put("api_key", api_key);
|
headerParams.put("api_key", ApiInvoker.parameterToString(api_key));
|
||||||
|
|
||||||
String[] contentTypes = {
|
String[] contentTypes = {
|
||||||
|
|
||||||
@ -428,7 +428,7 @@ public class PetApi {
|
|||||||
FormDataMultiPart mp = new FormDataMultiPart();
|
FormDataMultiPart mp = new FormDataMultiPart();
|
||||||
|
|
||||||
hasFields = true;
|
hasFields = true;
|
||||||
mp.field("additionalMetadata", additionalMetadata, MediaType.MULTIPART_FORM_DATA_TYPE);
|
mp.field("additionalMetadata", ApiInvoker.parameterToString(additionalMetadata), MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||||
|
|
||||||
hasFields = true;
|
hasFields = true;
|
||||||
mp.field("file", file, MediaType.MULTIPART_FORM_DATA_TYPE);
|
mp.field("file", file, MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||||
@ -437,7 +437,7 @@ public class PetApi {
|
|||||||
postBody = mp;
|
postBody = mp;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
formParams.put("additionalMetadata", additionalMetadata);
|
formParams.put("additionalMetadata", ApiInvoker.parameterToString(additionalMetadata));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -198,10 +198,10 @@ public class UserApi {
|
|||||||
Map<String, String> headerParams = new HashMap<String, String>();
|
Map<String, String> headerParams = new HashMap<String, String>();
|
||||||
Map<String, String> formParams = new HashMap<String, String>();
|
Map<String, String> formParams = new HashMap<String, String>();
|
||||||
|
|
||||||
if(!"null".equals(String.valueOf(username)))
|
if (username != null)
|
||||||
queryParams.put("username", String.valueOf(username));
|
queryParams.put("username", ApiInvoker.parameterToString(username));
|
||||||
if(!"null".equals(String.valueOf(password)))
|
if (password != null)
|
||||||
queryParams.put("password", String.valueOf(password));
|
queryParams.put("password", ApiInvoker.parameterToString(password));
|
||||||
|
|
||||||
|
|
||||||
String[] contentTypes = {
|
String[] contentTypes = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user