Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 of 6 tasks
Elewyth opened this issue Aug 11, 2020 · 0 comments
Closed
4 of 6 tasks

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

Elewyth opened this issue Aug 11, 2020 · 0 comments

Comments

@Elewyth
Copy link
Contributor

Elewyth commented Aug 11, 2020

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()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants