mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-19 07:07:11 +00:00
Fix Gson parsing of Joda DateTime without millis (#4473)
* Fix Gson parsing of Joda DateTime without millis The DateTimeFormatter returned by ISODateTimeFormat.dateTime() only parses dates with millisecond values, and throws IllegalArgumentException when milliseconds are not present. The date-time construct from RFC 3339 Section 5.6 referenced by the Swagger/OpenAPI spec allows fractional second values to be omitted. This results in valid date-time values being rejected by the generated code. This commit fixes the problem by using .dateOptionalTimeParser() for parsing, which correctly handles date-time values without fractional seconds. A previous version of this commit used .dateTimeParser(), which accepted a time without a date and was considered too liberal. Note that .dateTime() must still be used for printing, which is not supported by .dateTimeParser(). Signed-off-by: Kevin Locke <kevin@kevinlocke.name> * Fix akka-scala date-time parser with Joda As in the previous commit, which fixed Java generators, ISOISODateTimeFormat.dateOptionalTimeParser() should be used for date-time parsing and ISOISODateTimeFormat.dateTime() for printing. Apply the same change to akka-scala. Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
This commit is contained in:
@@ -172,14 +172,15 @@ class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,7 +192,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -354,14 +354,15 @@ class GsonConverterWrapper implements Converter {
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,7 +374,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -374,14 +374,15 @@ class GsonCustomConverterFactory extends Converter.Factory
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,7 +394,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,10 +85,10 @@ object ApiInvoker {
|
|||||||
|
|
||||||
case object DateTimeSerializer extends CustomSerializer[DateTime](format => ( {
|
case object DateTimeSerializer extends CustomSerializer[DateTime](format => ( {
|
||||||
case JString(s) =>
|
case JString(s) =>
|
||||||
ISODateTimeFormat.dateTimeParser().parseDateTime(s)
|
ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(s)
|
||||||
}, {
|
}, {
|
||||||
case d: DateTime =>
|
case d: DateTime =>
|
||||||
JString(ISODateTimeFormat.dateTimeParser().print(d))
|
JString(ISODateTimeFormat.dateTime().print(d))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -170,14 +170,15 @@ class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +190,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,10 +85,10 @@ object ApiInvoker {
|
|||||||
|
|
||||||
case object DateTimeSerializer extends CustomSerializer[DateTime](format => ( {
|
case object DateTimeSerializer extends CustomSerializer[DateTime](format => ( {
|
||||||
case JString(s) =>
|
case JString(s) =>
|
||||||
ISODateTimeFormat.dateTimeParser().parseDateTime(s)
|
ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(s)
|
||||||
}, {
|
}, {
|
||||||
case d: DateTime =>
|
case d: DateTime =>
|
||||||
JString(ISODateTimeFormat.dateTimeParser().print(d))
|
JString(ISODateTimeFormat.dateTime().print(d))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -182,14 +182,15 @@ class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +202,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,14 +170,15 @@ class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +190,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -362,14 +362,15 @@ class GsonCustomConverterFactory extends Converter.Factory
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -381,7 +382,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -362,14 +362,15 @@ class GsonCustomConverterFactory extends Converter.Factory
|
|||||||
*/
|
*/
|
||||||
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||||
|
|
||||||
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
|
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
|
||||||
|
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime date) throws IOException {
|
public void write(JsonWriter out, DateTime date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
out.value(formatter.print(date));
|
out.value(printFormatter.print(date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -381,7 +382,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
String date = in.nextString();
|
||||||
return formatter.parseDateTime(date);
|
return parseFormatter.parseDateTime(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user