Skip to content

[BUG][Java] DateFormat ist not used when serializing OffsetDateTime #7185

@Elewyth

Description

@Elewyth

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The generated ApiClient.java (using <dateLibrary>java8</dateLibrary> and <library>resttemplate</library>) contains a method parameterToString, which is used to convert arbitrary e.g. query-parameters to a string-representation. This method always checks for java.util.Date to apply the DateFormat, which will never match when using OffsetDateTime (<dateLibrary>java8</dateLibrary>). Therefore, String.valueOf(..) is used, which does not yield the same result as using the RFC3339DateFormat.

openapi-generator version

4.2.0

OpenAPI declaration file content or url

irrelevant

Generation Details
<configOptions>
  <dateLibrary>java8</dateLibrary>
  <library>resttemplate</library>
</configOptions>
Steps to reproduce
  1. Generate client classes from any schema
  2. Look at ApiClient#parameterToString
  3. See that it checks on old java.util.Date instead of OffsetDateTime.

See the following test-case to show that the output is not identical and might therefor fail on a subsequent parse:

public class DateFormatTest {

    @Test
    void test() {
        var date = OffsetDateTime.now();
        var date2 = Date.from(date.toInstant());
        assertEquals(new RFC3339DateFormat().format(date2), String.valueOf(date));
    }
}
Expected :2020-08-11T14:16:40.013Z
Actual   :2020-08-11T16:16:40.013502900+02:00
Related issues/PRs

Not that I could find any.

Suggest a fix

When <dateLibrary>java8</dateLibrary> is provided the date-formatting in the ApiClient-template should check for OffsetDateTime, instead of java.util.Date in parameterToString.

A less intrusive fix would be adding the following else-if to the checks in parameterToString:

} else if (param instanceof OffsetDateTime) {
        return formatDate(Date.from(((OffsetDateTime) param).toInstant()));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions