Open
Description
Description
Using dateLibrary
option with java8-localdatetime
value generates JSON.java
with Gson type adapter for parsing LocalDate
not LocalDateTime
(as expected).
As a result, this option is useless for parsing date-time
fields (the common scenario).
Swagger-codegen version
2.2.3
Command line used for generation
Options:
- language:
java
- dateLibrary:
java8-localdatetime
Suggestion for a fix
Add also LocalDateTime
adapter. Both types should be supported (as they are supported by Swagger/OpenAPI). And LocalDateTime
is the more useful type.
Workaround
In user code, create the correct type adapter:
class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
@Override
public void write(JsonWriter out, LocalDateTime date) throws IOException {
if (date == null) {
out.nullValue();
} else {
out.value(formatter.format(date));
}
}
@Override
public LocalDateTime read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
return LocalDateTime.parse(date, formatter);
}
}
}
And register it with Gson:
apiClient.getJSON().setGson(
new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter()).create());