Search code examples
openapi-generatoropenapi-generator-maven-plugin

Date parsing errors when timezone does not exist with Java OpenAPI generator client


I'm working on integrating with an API that (unfortunately) does not always append the timezone offset to their date. I know this isn't optimal but I can't change their behavior.

Example:

2022-06-08T16:07:13.96

Using the java generator and the java8 date library produces a runtime error while parsing the date:

java.time.format.DateTimeParseException: Text '2022-06-08T16:07:13.96' could not be parsed at index 22
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:404)
    at com.acme.openapi.JSON$OffsetDateTimeTypeAdapter.read(JSON.java:287)

From my limited understanding, I believe that ISO-8601 dates should be treated as local dates instead of offset dates if the zone offset is omitted. Im unsure if this is something that is supported in the java generator.


Solution

  • I ended up swapping the date library to java8-localdatetime but it gave GSON some problems:

    Expected BEGIN_OBJECT but was String
    

    I changed to a jackson based client and that cleared everything up.