Merge branch 'master' into clojure-client

This commit is contained in:
xhh 2015-11-17 00:47:49 +08:00
commit 473d34ef76
18 changed files with 460 additions and 92 deletions

View File

@ -55,13 +55,15 @@ public class ApiClient {
private DateFormat dateFormat; private DateFormat dateFormat;
public ApiClient() { public ApiClient() {
// Use ISO 8601 format for date and datetime. // Use RFC3339 format for date and datetime.
// See https://en.wikipedia.org/wiki/ISO_8601 // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone. // Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.json.setDateFormat((DateFormat) dateFormat.clone());
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
@ -74,6 +76,13 @@ public class ApiClient {
authentications = Collections.unmodifiableMap(authentications); authentications = Collections.unmodifiableMap(authentications);
} }
/**
* Gets the JSON instance to do JSON serialization and deserialization.
*/
public JSON getJSON() {
return json;
}
public String getBasePath() { public String getBasePath() {
return basePath; return basePath;
} }
@ -227,6 +236,8 @@ public class ApiClient {
*/ */
public ApiClient setDateFormat(DateFormat dateFormat) { public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
// also set the date format for model (de)serialization with Date properties
this.json.setDateFormat((DateFormat) dateFormat.clone());
return this; return this;
} }

View File

@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.*; import com.fasterxml.jackson.datatype.joda.*;
import java.text.DateFormat;
import java.io.IOException; import java.io.IOException;
{{>generatedAnnotation}} {{>generatedAnnotation}}
@ -20,6 +22,13 @@ public class JSON {
mapper.registerModule(new JodaModule()); mapper.registerModule(new JodaModule());
} }
/**
* Set the date format for JSON (de)serialization with Date properties.
*/
public void setDateFormat(DateFormat dateFormat) {
mapper.setDateFormat(dateFormat);
}
/** /**
* Serialize the given Java object into JSON string. * Serialize the given Java object into JSON string.
*/ */

View File

@ -60,13 +60,15 @@ public class ApiClient {
private DateFormat dateFormat; private DateFormat dateFormat;
public ApiClient() { public ApiClient() {
// Use ISO 8601 format for date and datetime. // Use RFC3339 format for date and datetime.
// See https://en.wikipedia.org/wiki/ISO_8601 // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone. // Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.json.setDateFormat((DateFormat) dateFormat.clone());
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
@ -81,6 +83,13 @@ public class ApiClient {
authentications = Collections.unmodifiableMap(authentications); authentications = Collections.unmodifiableMap(authentications);
} }
/**
* Gets the JSON instance to do JSON serialization and deserialization.
*/
public JSON getJSON() {
return json;
}
public String getBasePath() { public String getBasePath() {
return basePath; return basePath;
} }
@ -235,6 +244,8 @@ public class ApiClient {
*/ */
public ApiClient setDateFormat(DateFormat dateFormat) { public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
// also set the date format for model (de)serialization with Date properties
this.json.setDateFormat((DateFormat) dateFormat.clone());
return this; return this;
} }

View File

@ -75,13 +75,11 @@ public class ApiClient {
private int statusCode; private int statusCode;
private Map<String, List<String>> responseHeaders; private Map<String, List<String>> responseHeaders;
private String dateFormat; private DateFormat dateFormat;
private DateFormat dateFormatter; private DateFormat datetimeFormat;
private boolean lenientDatetimeFormat;
private int dateLength; private int dateLength;
private String datetimeFormat;
private DateFormat datetimeFormatter;
private InputStream sslCaCert; private InputStream sslCaCert;
private boolean verifyingSsl; private boolean verifyingSsl;
@ -95,10 +93,19 @@ public class ApiClient {
json = new JSON(this); json = new JSON(this);
// Use ISO 8601 format for date and datetime. /*
// See https://en.wikipedia.org/wiki/ISO_8601 * Use RFC3339 format for date and datetime.
setDateFormat("yyyy-MM-dd"); * See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
setDatetimeFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); */
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// Always use UTC as the default time zone when dealing with date (without time).
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// Use the system's default time zone when dealing with datetime (mainly formatting).
this.datetimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Be lenient on datetime formats when parsing datetime from string.
// See <code>parseDatetime</code>.
this.lenientDatetimeFormat = true;
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
@ -186,32 +193,35 @@ public class ApiClient {
return this; return this;
} }
public String getDateFormat() { public DateFormat getDateFormat() {
return dateFormat; return dateFormat;
} }
public ApiClient setDateFormat(String dateFormat) { public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
this.dateLength = this.dateFormat.format(new Date()).length();
this.dateFormatter = new SimpleDateFormat(dateFormat);
// Use UTC as the default time zone.
this.dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
this.dateLength = this.dateFormatter.format(new Date()).length();
return this; return this;
} }
public String getDatetimeFormat() { public DateFormat getDatetimeFormat() {
return datetimeFormat; return datetimeFormat;
} }
public ApiClient setDatetimeFormat(String datetimeFormat) { public ApiClient setDatetimeFormat(DateFormat datetimeFormat) {
this.datetimeFormat = datetimeFormat; this.datetimeFormat = datetimeFormat;
return this;
}
this.datetimeFormatter = new SimpleDateFormat(datetimeFormat); /**
// Note: The datetime formatter uses the system's default time zone. * Whether to allow various ISO 8601 datetime formats when parsing a datetime string.
* @see #parseDatetime(String)
*/
public boolean isLenientDatetimeFormat() {
return lenientDatetimeFormat;
}
public ApiClient setLenientDatetimeFormat(boolean lenientDatetimeFormat) {
this.lenientDatetimeFormat = lenientDatetimeFormat;
return this; return this;
} }
@ -225,15 +235,15 @@ public class ApiClient {
if (str == null) if (str == null)
return null; return null;
try { try {
return dateFormatter.parse(str); return dateFormat.parse(str);
} catch (ParseException e) { } catch (ParseException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
/** /**
* Parse the given date-time string into Date object. * Parse the given datetime string into Date object.
* The default <code>datetimeFormat</code> supports these ISO 8601 datetime formats: * When <code>lenientDatetimeFormat</code> is enabled, the following ISO 8601 datetime formats are supported:
* 2015-08-16T08:20:05Z * 2015-08-16T08:20:05Z
* 2015-8-16T8:20:05Z * 2015-8-16T8:20:05Z
* 2015-08-16T08:20:05+00:00 * 2015-08-16T08:20:05+00:00
@ -253,25 +263,25 @@ public class ApiClient {
if (str == null) if (str == null)
return null; return null;
if ("yyyy-MM-dd'T'HH:mm:ss.SSSZ".equals(datetimeFormat)) { if (lenientDatetimeFormat) {
/* /*
* When the default datetime format is used, process the given string * When lenientDatetimeFormat is enabled, process the given string
* to support various formats defined by ISO 8601. * to support various formats defined by ISO 8601.
*/ */
// normalize time zone // normalize time zone
// trailing "Z": 2015-08-16T08:20:05Z => 2015-08-16T08:20:05+0000 // trailing "Z": 2015-08-16T08:20:05Z => 2015-08-16T08:20:05+00:00
str = str.replaceAll("[zZ]\\z", "+0000"); str = str.replaceAll("[zZ]\\z", "+00:00");
// remove colon: 2015-08-16T08:20:05+00:00 => 2015-08-16T08:20:05+0000 // add colon: 2015-08-16T08:20:05+0000 => 2015-08-16T08:20:05+00:00
str = str.replaceAll("([+-]\\d{2}):(\\d{2})\\z", "$1$2"); str = str.replaceAll("([+-]\\d{2})(\\d{2})\\z", "$1:$2");
// expand time zone: 2015-08-16T08:20:05+00 => 2015-08-16T08:20:05+0000 // expand time zone: 2015-08-16T08:20:05+00 => 2015-08-16T08:20:05+00:00
str = str.replaceAll("([+-]\\d{2})\\z", "$100"); str = str.replaceAll("([+-]\\d{2})\\z", "$1:00");
// add milliseconds when missing // add milliseconds when missing
// 2015-08-16T08:20:05+0000 => 2015-08-16T08:20:05.000+0000 // 2015-08-16T08:20:05+00:00 => 2015-08-16T08:20:05.000+00:00
str = str.replaceAll("(:\\d{1,2})([+-]\\d{4})\\z", "$1.000$2"); str = str.replaceAll("(:\\d{1,2})([+-]\\d{2}:\\d{2})\\z", "$1.000$2");
} }
try { try {
return datetimeFormatter.parse(str); return datetimeFormat.parse(str);
} catch (ParseException e) { } catch (ParseException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -290,14 +300,14 @@ public class ApiClient {
* Format the given Date object into string. * Format the given Date object into string.
*/ */
public String formatDate(Date date) { public String formatDate(Date date) {
return dateFormatter.format(date); return dateFormat.format(date);
} }
/** /**
* Format the given Date object into string. * Format the given Date object into string.
*/ */
public String formatDatetime(Date date) { public String formatDatetime(Date date) {
return datetimeFormatter.format(date); return datetimeFormat.format(date);
} }
/** /**

View File

@ -39,7 +39,7 @@ import io.swagger.client.auth.HttpBasicAuth;
import io.swagger.client.auth.ApiKeyAuth; import io.swagger.client.auth.ApiKeyAuth;
import io.swagger.client.auth.OAuth; import io.swagger.client.auth.OAuth;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-02T21:16:46.418+08:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-07T15:05:10.376+08:00")
public class ApiClient { public class ApiClient {
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>();
@ -55,13 +55,15 @@ public class ApiClient {
private DateFormat dateFormat; private DateFormat dateFormat;
public ApiClient() { public ApiClient() {
// Use ISO 8601 format for date and datetime. // Use RFC3339 format for date and datetime.
// See https://en.wikipedia.org/wiki/ISO_8601 // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone. // Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.json.setDateFormat((DateFormat) dateFormat.clone());
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
@ -73,6 +75,13 @@ public class ApiClient {
authentications = Collections.unmodifiableMap(authentications); authentications = Collections.unmodifiableMap(authentications);
} }
/**
* Gets the JSON instance to do JSON serialization and deserialization.
*/
public JSON getJSON() {
return json;
}
public String getBasePath() { public String getBasePath() {
return basePath; return basePath;
} }
@ -226,6 +235,8 @@ public class ApiClient {
*/ */
public ApiClient setDateFormat(DateFormat dateFormat) { public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
// also set the date format for model (de)serialization with Date properties
this.json.setDateFormat((DateFormat) dateFormat.clone());
return this; return this;
} }

View File

@ -4,9 +4,11 @@ import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.*; import com.fasterxml.jackson.datatype.joda.*;
import java.text.DateFormat;
import java.io.IOException; import java.io.IOException;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-30T16:36:47.681+08:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-07T15:05:10.376+08:00")
public class JSON { public class JSON {
private ObjectMapper mapper; private ObjectMapper mapper;
@ -20,6 +22,13 @@ public class JSON {
mapper.registerModule(new JodaModule()); mapper.registerModule(new JodaModule());
} }
/**
* Set the date format for JSON (de)serialization with Date properties.
*/
public void setDateFormat(DateFormat dateFormat) {
mapper.setDateFormat(dateFormat);
}
/** /**
* Serialize the given Java object into JSON string. * Serialize the given Java object into JSON string.
*/ */

View File

@ -2,6 +2,8 @@ package io.swagger.client;
import io.swagger.client.auth.*; import io.swagger.client.auth.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import org.junit.*; import org.junit.*;
@ -16,6 +18,26 @@ public class ApiClientTest {
apiClient = new ApiClient(); apiClient = new ApiClient();
} }
@Test
public void testParseAndFormatDate() {
// default date format
String dateStr = "2015-11-07T03:49:09.356Z";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356+00:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356Z")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T05:49:09.356+02:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T02:49:09.356-01:00")));
// custom date format: without milli-seconds, custom time zone
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
format.setTimeZone(TimeZone.getTimeZone("GMT+10"));
apiClient.setDateFormat(format);
dateStr = "2015-11-07T13:49:09+10:00";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09+00:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09Z")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T00:49:09-03:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T13:49:09+10:00")));
}
@Test @Test
public void testSelectHeaderAccept() { public void testSelectHeaderAccept() {
String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"}; String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"};

View File

@ -0,0 +1,52 @@
package io.swagger.client;
import io.swagger.client.model.Order;
import java.lang.Exception;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.TimeZone;
import org.junit.*;
import static org.junit.Assert.*;
public class JSONTest {
JSON json = null;
Order order = null;
@Before
public void setup() {
json = new JSON();
order = new Order();
}
@Test
public void testDefaultDate() throws Exception {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
final String dateStr = "2015-11-07T14:11:05.267Z";
order.setShipDate(dateFormat.parse(dateStr));
String str = json.serialize(order);
TypeRef typeRef = new TypeRef<Order>() { };
Order o = json.deserialize(str, typeRef);
assertEquals(dateStr, dateFormat.format(o.getShipDate()));
}
@Test
public void testCustomDate() throws Exception {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-2"));
final String dateStr = "2015-11-07T14:11:05-02:00";
order.setShipDate(dateFormat.parse(dateStr));
json.setDateFormat(dateFormat);
String str = json.serialize(order);
TypeRef typeRef = new TypeRef<Order>() { };
Order o = json.deserialize(str, typeRef);
assertEquals(dateStr, dateFormat.format(o.getShipDate()));
}
}

View File

@ -8,6 +8,7 @@ import io.swagger.client.auth.*;
import io.swagger.client.model.*; import io.swagger.client.model.*;
import java.util.Map; import java.util.Map;
import java.text.SimpleDateFormat;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -21,6 +22,8 @@ public class StoreApiTest {
// setup authentication // setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key"); apiKeyAuth.setApiKey("special-key");
// set custom date format that is used by the petstore server
api.getApiClient().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
} }
@Test @Test

View File

@ -43,7 +43,7 @@ import io.swagger.client.auth.HttpBasicAuth;
import io.swagger.client.auth.ApiKeyAuth; import io.swagger.client.auth.ApiKeyAuth;
import io.swagger.client.auth.OAuth; import io.swagger.client.auth.OAuth;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-02T22:10:35.641+08:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-09T11:02:32.553+08:00")
public class ApiClient { public class ApiClient {
private Client client; private Client client;
private Map<String, Client> hostMap = new HashMap<String, Client>(); private Map<String, Client> hostMap = new HashMap<String, Client>();
@ -60,13 +60,15 @@ public class ApiClient {
private DateFormat dateFormat; private DateFormat dateFormat;
public ApiClient() { public ApiClient() {
// Use ISO 8601 format for date and datetime. // Use RFC3339 format for date and datetime.
// See https://en.wikipedia.org/wiki/ISO_8601 // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone. // Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.json.setDateFormat((DateFormat) dateFormat.clone());
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
@ -80,6 +82,13 @@ public class ApiClient {
authentications = Collections.unmodifiableMap(authentications); authentications = Collections.unmodifiableMap(authentications);
} }
/**
* Gets the JSON instance to do JSON serialization and deserialization.
*/
public JSON getJSON() {
return json;
}
public String getBasePath() { public String getBasePath() {
return basePath; return basePath;
} }
@ -234,6 +243,8 @@ public class ApiClient {
*/ */
public ApiClient setDateFormat(DateFormat dateFormat) { public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
// also set the date format for model (de)serialization with Date properties
this.json.setDateFormat((DateFormat) dateFormat.clone());
return this; return this;
} }

View File

@ -4,9 +4,11 @@ import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.*; import com.fasterxml.jackson.datatype.joda.*;
import java.text.DateFormat;
import java.io.IOException; import java.io.IOException;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-21T11:55:20.020+08:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-09T11:02:32.553+08:00")
public class JSON { public class JSON {
private ObjectMapper mapper; private ObjectMapper mapper;
@ -20,6 +22,13 @@ public class JSON {
mapper.registerModule(new JodaModule()); mapper.registerModule(new JodaModule());
} }
/**
* Set the date format for JSON (de)serialization with Date properties.
*/
public void setDateFormat(DateFormat dateFormat) {
mapper.setDateFormat(dateFormat);
}
/** /**
* Serialize the given Java object into JSON string. * Serialize the given Java object into JSON string.
*/ */

View File

@ -2,6 +2,8 @@ package io.swagger.client;
import io.swagger.client.auth.*; import io.swagger.client.auth.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import org.junit.*; import org.junit.*;
@ -16,6 +18,26 @@ public class ApiClientTest {
apiClient = new ApiClient(); apiClient = new ApiClient();
} }
@Test
public void testParseAndFormatDate() {
// default date format
String dateStr = "2015-11-07T03:49:09.356Z";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356+00:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356Z")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T05:49:09.356+02:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T02:49:09.356-01:00")));
// custom date format: without milli-seconds, custom time zone
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
format.setTimeZone(TimeZone.getTimeZone("GMT+10"));
apiClient.setDateFormat(format);
dateStr = "2015-11-07T13:49:09+10:00";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09+00:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09Z")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T00:49:09-03:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T13:49:09+10:00")));
}
@Test @Test
public void testSelectHeaderAccept() { public void testSelectHeaderAccept() {
String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"}; String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"};

View File

@ -0,0 +1,52 @@
package io.swagger.client;
import io.swagger.client.model.Order;
import java.lang.Exception;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.TimeZone;
import org.junit.*;
import static org.junit.Assert.*;
public class JSONTest {
JSON json = null;
Order order = null;
@Before
public void setup() {
json = new JSON();
order = new Order();
}
@Test
public void testDefaultDate() throws Exception {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
final String dateStr = "2015-11-07T14:11:05.267Z";
order.setShipDate(dateFormat.parse(dateStr));
String str = json.serialize(order);
TypeRef typeRef = new TypeRef<Order>() { };
Order o = json.deserialize(str, typeRef);
assertEquals(dateStr, dateFormat.format(o.getShipDate()));
}
@Test
public void testCustomDate() throws Exception {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-2"));
final String dateStr = "2015-11-07T14:11:05-02:00";
order.setShipDate(dateFormat.parse(dateStr));
json.setDateFormat(dateFormat);
String str = json.serialize(order);
TypeRef typeRef = new TypeRef<Order>() { };
Order o = json.deserialize(str, typeRef);
assertEquals(dateStr, dateFormat.format(o.getShipDate()));
}
}

View File

@ -8,6 +8,7 @@ import io.swagger.client.auth.*;
import io.swagger.client.model.*; import io.swagger.client.model.*;
import java.util.Map; import java.util.Map;
import java.text.SimpleDateFormat;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -21,6 +22,8 @@ public class StoreApiTest {
// setup authentication // setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key"); apiKeyAuth.setApiKey("special-key");
// set custom date format that is used by the petstore server
api.getApiClient().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
} }
@Test @Test

View File

@ -75,13 +75,11 @@ public class ApiClient {
private int statusCode; private int statusCode;
private Map<String, List<String>> responseHeaders; private Map<String, List<String>> responseHeaders;
private String dateFormat; private DateFormat dateFormat;
private DateFormat dateFormatter; private DateFormat datetimeFormat;
private boolean lenientDatetimeFormat;
private int dateLength; private int dateLength;
private String datetimeFormat;
private DateFormat datetimeFormatter;
private InputStream sslCaCert; private InputStream sslCaCert;
private boolean verifyingSsl; private boolean verifyingSsl;
@ -95,10 +93,19 @@ public class ApiClient {
json = new JSON(this); json = new JSON(this);
// Use ISO 8601 format for date and datetime. /*
// See https://en.wikipedia.org/wiki/ISO_8601 * Use RFC3339 format for date and datetime.
setDateFormat("yyyy-MM-dd"); * See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
setDatetimeFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); */
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// Always use UTC as the default time zone when dealing with date (without time).
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// Use the system's default time zone when dealing with datetime (mainly formatting).
this.datetimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Be lenient on datetime formats when parsing datetime from string.
// See <code>parseDatetime</code>.
this.lenientDatetimeFormat = true;
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-Swagger"); setUserAgent("Java-Swagger");
@ -185,32 +192,35 @@ public class ApiClient {
return this; return this;
} }
public String getDateFormat() { public DateFormat getDateFormat() {
return dateFormat; return dateFormat;
} }
public ApiClient setDateFormat(String dateFormat) { public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
this.dateLength = this.dateFormat.format(new Date()).length();
this.dateFormatter = new SimpleDateFormat(dateFormat);
// Use UTC as the default time zone.
this.dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
this.dateLength = this.dateFormatter.format(new Date()).length();
return this; return this;
} }
public String getDatetimeFormat() { public DateFormat getDatetimeFormat() {
return datetimeFormat; return datetimeFormat;
} }
public ApiClient setDatetimeFormat(String datetimeFormat) { public ApiClient setDatetimeFormat(DateFormat datetimeFormat) {
this.datetimeFormat = datetimeFormat; this.datetimeFormat = datetimeFormat;
return this;
}
this.datetimeFormatter = new SimpleDateFormat(datetimeFormat); /**
// Note: The datetime formatter uses the system's default time zone. * Whether to allow various ISO 8601 datetime formats when parsing a datetime string.
* @see #parseDatetime(String)
*/
public boolean isLenientDatetimeFormat() {
return lenientDatetimeFormat;
}
public ApiClient setLenientDatetimeFormat(boolean lenientDatetimeFormat) {
this.lenientDatetimeFormat = lenientDatetimeFormat;
return this; return this;
} }
@ -224,15 +234,15 @@ public class ApiClient {
if (str == null) if (str == null)
return null; return null;
try { try {
return dateFormatter.parse(str); return dateFormat.parse(str);
} catch (ParseException e) { } catch (ParseException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
/** /**
* Parse the given date-time string into Date object. * Parse the given datetime string into Date object.
* The default <code>datetimeFormat</code> supports these ISO 8601 datetime formats: * When <code>lenientDatetimeFormat</code> is enabled, the following ISO 8601 datetime formats are supported:
* 2015-08-16T08:20:05Z * 2015-08-16T08:20:05Z
* 2015-8-16T8:20:05Z * 2015-8-16T8:20:05Z
* 2015-08-16T08:20:05+00:00 * 2015-08-16T08:20:05+00:00
@ -252,25 +262,25 @@ public class ApiClient {
if (str == null) if (str == null)
return null; return null;
if ("yyyy-MM-dd'T'HH:mm:ss.SSSZ".equals(datetimeFormat)) { if (lenientDatetimeFormat) {
/* /*
* When the default datetime format is used, process the given string * When lenientDatetimeFormat is enabled, process the given string
* to support various formats defined by ISO 8601. * to support various formats defined by ISO 8601.
*/ */
// normalize time zone // normalize time zone
// trailing "Z": 2015-08-16T08:20:05Z => 2015-08-16T08:20:05+0000 // trailing "Z": 2015-08-16T08:20:05Z => 2015-08-16T08:20:05+00:00
str = str.replaceAll("[zZ]\\z", "+0000"); str = str.replaceAll("[zZ]\\z", "+00:00");
// remove colon: 2015-08-16T08:20:05+00:00 => 2015-08-16T08:20:05+0000 // add colon: 2015-08-16T08:20:05+0000 => 2015-08-16T08:20:05+00:00
str = str.replaceAll("([+-]\\d{2}):(\\d{2})\\z", "$1$2"); str = str.replaceAll("([+-]\\d{2})(\\d{2})\\z", "$1:$2");
// expand time zone: 2015-08-16T08:20:05+00 => 2015-08-16T08:20:05+0000 // expand time zone: 2015-08-16T08:20:05+00 => 2015-08-16T08:20:05+00:00
str = str.replaceAll("([+-]\\d{2})\\z", "$100"); str = str.replaceAll("([+-]\\d{2})\\z", "$1:00");
// add milliseconds when missing // add milliseconds when missing
// 2015-08-16T08:20:05+0000 => 2015-08-16T08:20:05.000+0000 // 2015-08-16T08:20:05+00:00 => 2015-08-16T08:20:05.000+00:00
str = str.replaceAll("(:\\d{1,2})([+-]\\d{4})\\z", "$1.000$2"); str = str.replaceAll("(:\\d{1,2})([+-]\\d{2}:\\d{2})\\z", "$1.000$2");
} }
try { try {
return datetimeFormatter.parse(str); return datetimeFormat.parse(str);
} catch (ParseException e) { } catch (ParseException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -289,14 +299,14 @@ public class ApiClient {
* Format the given Date object into string. * Format the given Date object into string.
*/ */
public String formatDate(Date date) { public String formatDate(Date date) {
return dateFormatter.format(date); return dateFormat.format(date);
} }
/** /**
* Format the given Date object into string. * Format the given Date object into string.
*/ */
public String formatDatetime(Date date) { public String formatDatetime(Date date) {
return datetimeFormatter.format(date); return datetimeFormat.format(date);
} }
/** /**

View File

@ -2,7 +2,10 @@ package io.swagger.client;
import io.swagger.client.auth.*; import io.swagger.client.auth.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.TimeZone;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -16,6 +19,64 @@ public class ApiClientTest {
apiClient = new ApiClient(); apiClient = new ApiClient();
} }
@Test
public void testParseAndFormatDatetime() {
// default datetime format with UTC time zone
apiClient.getDatetimeFormat().setTimeZone(TimeZone.getTimeZone("UTC"));
String dateStr = "2015-11-07T03:49:09.356Z";
assertTrue(apiClient.isLenientDatetimeFormat());
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T03:49:09.356+00:00")));
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T05:49:09.356+02:00")));
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T02:49:09.356-01:00")));
// support various cases
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T03:49:09.356Z")));
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T03:49:09.356+00")));
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T02:49:09.356-0100")));
dateStr = "2015-11-07T03:49:09.000Z";
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T05:49:09+02")));
// custom datetime format: without milli-seconds, custom time zone
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
format.setTimeZone(TimeZone.getTimeZone("GMT+10"));
apiClient.setDatetimeFormat(format);
// disable support of various datetime format
apiClient.setLenientDatetimeFormat(false);
dateStr = "2015-11-07T13:49:09+10:00";
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T03:49:09+00:00")));
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T03:49:09Z")));
assertEquals(dateStr, apiClient.formatDatetime(apiClient.parseDatetime("2015-11-07T00:49:09-03:00")));
try {
// invalid time zone format
apiClient.parseDatetime("2015-11-07T03:49:09+00");
fail("parseDatetime should fail");
} catch (RuntimeException e) {
// OK
}
try {
// unexpected miliseconds
apiClient.parseDatetime("2015-11-07T03:49:09.000Z");
fail("parseDatetime should fail");
} catch (RuntimeException e) {
// OK
}
}
@Test
public void testParseAndFormatDate() {
// default date format
String dateStr = "2015-11-07";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDatetime("2015-11-07T03:49:09.356+00:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07")));
// custom date format: without day
DateFormat format = new SimpleDateFormat("yyyy-MM");
apiClient.setDateFormat(format);
dateStr = "2015-11";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDatetime("2015-11-07T03:49:09Z")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11")));
}
@Test @Test
public void testSelectHeaderAccept() { public void testSelectHeaderAccept() {
String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"}; String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"};

View File

@ -0,0 +1,55 @@
package io.swagger.client;
import com.google.gson.reflect.TypeToken;
import io.swagger.client.model.Order;
import java.lang.Exception;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;
public class JSONTest {
ApiClient apiClient = null;
JSON json = null;
Order order = null;
@Before
public void setup() {
apiClient = new ApiClient();
json = new JSON(apiClient);
order = new Order();
}
@Test
public void testDefaultDate() throws Exception {
final DateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
datetimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
final String dateStr = "2015-11-07T14:11:05.267Z";
order.setShipDate(datetimeFormat.parse(dateStr));
String str = json.serialize(order);
Type type = new TypeToken<Order>() { }.getType();
Order o = json.deserialize(str, type);
assertEquals(dateStr, datetimeFormat.format(o.getShipDate()));
}
@Test
public void testCustomDate() throws Exception {
final DateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
datetimeFormat.setTimeZone(TimeZone.getTimeZone("GMT-2"));
final String dateStr = "2015-11-07T14:11:05-02:00";
order.setShipDate(datetimeFormat.parse(dateStr));
apiClient.setDatetimeFormat(datetimeFormat);
apiClient.setLenientDatetimeFormat(false);
String str = json.serialize(order);
Type type = new TypeToken<Order>() { }.getType();
Order o = json.deserialize(str, type);
assertEquals(dateStr, datetimeFormat.format(o.getShipDate()));
}
}

View File

@ -8,6 +8,7 @@ import io.swagger.client.auth.*;
import io.swagger.client.model.*; import io.swagger.client.model.*;
import java.util.Map; import java.util.Map;
import java.text.SimpleDateFormat;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -21,6 +22,12 @@ public class StoreApiTest {
// setup authentication // setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key"); apiKeyAuth.setApiKey("special-key");
// set custom date format that is used by the petstore server
// Note: it would still work without this setting as okhttp-gson Java client supports
// various date formats by default (with lenientDatetimeFormat enabled), including
// the one used by petstore server
api.getApiClient().setDatetimeFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
api.getApiClient().setLenientDatetimeFormat(false);
} }
@Test @Test