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:
Kevin Locke
2017-01-07 06:56:14 -07:00
committed by wing328
parent 5686109cc9
commit 409e1a504c
10 changed files with 36 additions and 28 deletions

View File

@@ -170,14 +170,15 @@ class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
*/
class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
private final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
@Override
public void write(JsonWriter out, DateTime date) throws IOException {
if (date == null) {
out.nullValue();
} else {
out.value(formatter.print(date));
out.value(printFormatter.print(date));
}
}
@@ -189,7 +190,7 @@ class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
return null;
default:
String date = in.nextString();
return formatter.parseDateTime(date);
return parseFormatter.parseDateTime(date);
}
}
}