Skip to content

[Swift5] Fix Datetime default value#7003

Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom
allezxandre:fix-datetime-handling
Jul 24, 2020
Merged

[Swift5] Fix Datetime default value#7003
wing328 merged 2 commits intoOpenAPITools:masterfrom
allezxandre:fix-datetime-handling

Conversation

@allezxandre
Copy link
Copy Markdown
Contributor

@allezxandre allezxandre commented Jul 20, 2020

This is a follow-up to #5256, which happens to affect the Swift5 code-gen.

If a default value is provided by the API spec for a date-time attribute, this PR fixes the generation code to build a correct Swift Date object.

To close #5256

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language: @4brunu

If a default value is provided by the API spec for a date-time attribute,
this commit fixes the generation code to build a correct Swift `Date` object.
@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Jul 21, 2020

Can you please run ./mvnw package && ./bin/generate-samples.sh bin/configs/other/swift5-* and commit the changes?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jul 21, 2020

I've updated the samples via 80f9612. Let's see how that goes.

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Jul 21, 2020

It looks like the samples don't have a default date value, so this PR will be difficult to validate.
@allezxandre could you please test this changes locally?

public var mapMapString: [String: [String: String]]?

public init(mapString: [String: String]?, mapMapString: [String: [String: String]]?) {
public init(mapString: [String: String]? = nil, mapMapString: [String: [String: String]]? = nil) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allezxandre looks like many parameters are default to nil. Is that expected?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR, it was introduced in another one, but apparently the samples were outdated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tested with the master but didn't get these nil default value. Maybe my test wasn't done right.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test on master

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run ./mvnw package && ./bin/generate-samples.sh bin/configs/other/swift5-* on the master, I get all of those nil changes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming that.

Is the nil default value expected? If not, we can remove it later in another PR. ( I think I know where to fix it )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is expected.
Basically this is just providing default nil values for for nullable types to shorten the constructor to the library user.

@allezxandre
Copy link
Copy Markdown
Contributor Author

It looks like the samples don't have a default date value, so this PR will be difficult to validate.
@allezxandre could you please test this changes locally?

I tested this locally, and this yields the following default date when it is defined to March 17th 2020 10:00:00 UTC:

Date(timeIntervalSince1970: 1584439200000000.0 / 1_000_000)

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Jul 22, 2020

Is there any disadvantages in removing the division like this?
Date(timeIntervalSince1970: 1584439200)

@allezxandre
Copy link
Copy Markdown
Contributor Author

Is there any disadvantages in removing the division like this?
Date(timeIntervalSince1970: 1584439200)

This is because the date time can be expressed up to the microsecond precision. However, in Java they are represented as Microseconds stored in a long and in Swift they are represented as seconds represented in a double.

To avoid float precision issue, I let Java keep its storage as a long, and then add a .0 and let the Swift compiler divide by a million to get the seconds stored in a double.

In my example of a 1584439200.0 seconds timestamp, the date I used has 0 minutes, 0 seconds, 0 milliseconds, and 0 microseconds, so I guess it would be smarter in that case to remove the division. However, in other cases with dates that are not rounded like that, the division would prove useful.

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Jul 23, 2020

Thanks for the explanation, looks good to me 👍

@wing328 wing328 merged commit 83f64db into OpenAPITools:master Jul 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][JAVA][MAVEN] java.time.OffsetDateTime cannot be cast to java.lang.String

3 participants