forked from loafle/openapi-generator-original
Support customizing date format in model (de)serialization in Java client
and update the default date format to conform to RFC3339
This commit is contained in:
parent
a5878130c4
commit
c59be88a3e
@ -55,13 +55,15 @@ public class ApiClient {
|
||||
private DateFormat dateFormat;
|
||||
|
||||
public ApiClient() {
|
||||
// Use ISO 8601 format for date and datetime.
|
||||
// See https://en.wikipedia.org/wiki/ISO_8601
|
||||
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||
// Use RFC3339 format for date and datetime.
|
||||
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
|
||||
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
|
||||
|
||||
// Use UTC as the default time zone.
|
||||
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
this.json.setDateFormat((DateFormat) dateFormat.clone());
|
||||
|
||||
// Set default User-Agent.
|
||||
setUserAgent("Java-Swagger");
|
||||
|
||||
@ -74,6 +76,13 @@ public class ApiClient {
|
||||
authentications = Collections.unmodifiableMap(authentications);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the JSON instance to do JSON serialization and deserialization.
|
||||
*/
|
||||
public JSON getJSON() {
|
||||
return json;
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
@ -227,6 +236,8 @@ public class ApiClient {
|
||||
*/
|
||||
public ApiClient setDateFormat(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;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.datatype.joda.*;
|
||||
|
||||
import java.text.DateFormat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
{{>generatedAnnotation}}
|
||||
@ -20,6 +22,13 @@ public class JSON {
|
||||
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.
|
||||
*/
|
||||
|
@ -39,7 +39,7 @@ import io.swagger.client.auth.HttpBasicAuth;
|
||||
import io.swagger.client.auth.ApiKeyAuth;
|
||||
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 {
|
||||
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
@ -55,13 +55,15 @@ public class ApiClient {
|
||||
private DateFormat dateFormat;
|
||||
|
||||
public ApiClient() {
|
||||
// Use ISO 8601 format for date and datetime.
|
||||
// See https://en.wikipedia.org/wiki/ISO_8601
|
||||
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||
// Use RFC3339 format for date and datetime.
|
||||
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
|
||||
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
|
||||
|
||||
// Use UTC as the default time zone.
|
||||
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
this.json.setDateFormat((DateFormat) dateFormat.clone());
|
||||
|
||||
// Set default User-Agent.
|
||||
setUserAgent("Java-Swagger");
|
||||
|
||||
@ -73,6 +75,13 @@ public class ApiClient {
|
||||
authentications = Collections.unmodifiableMap(authentications);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the JSON instance to do JSON serialization and deserialization.
|
||||
*/
|
||||
public JSON getJSON() {
|
||||
return json;
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
@ -226,6 +235,8 @@ public class ApiClient {
|
||||
*/
|
||||
public ApiClient setDateFormat(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;
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,11 @@ import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.datatype.joda.*;
|
||||
|
||||
import java.text.DateFormat;
|
||||
|
||||
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 {
|
||||
private ObjectMapper mapper;
|
||||
|
||||
@ -20,6 +22,13 @@ public class JSON {
|
||||
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.
|
||||
*/
|
||||
|
@ -2,6 +2,8 @@ package io.swagger.client;
|
||||
|
||||
import io.swagger.client.auth.*;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import org.junit.*;
|
||||
@ -16,6 +18,26 @@ public class ApiClientTest {
|
||||
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
|
||||
public void testSelectHeaderAccept() {
|
||||
String[] accepts = {"APPLICATION/JSON", "APPLICATION/XML"};
|
||||
|
@ -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-0200";
|
||||
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()));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user