Skip to content

Date-only deserialization is not correct when local time uses negative UTC offset #444

Open

Description

When operationSpec for an API response specifies an object property with format "Date" its value is not deserialized to Javascript Date object as expected.

Swagger / Open API example:

"dateOfIssue": {
          "format": "date",
          "description": "Date of issue",
          "type": "string"
        },

For example when value "2021-03-31" is supplied by API in aforementioned property dateOfIssue the resulting Date object is actually deserialized as "2021-03-30" because the local timezone offset is applied.

Code in question is:

} else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/gi) !== null) {
payload = new Date(responseBody);

This is due to a rather strange behaviour of Javascript Date object when local time is set to negative UTC offset and only a date value is passed to its contructor:

// Timezone set to -6
new Date("2021-03-31")  // is in fact interpreted as "Tue Mar 30 2021 18:00:00 GMT-0600 (Central Standard Time)"

The above issue does not apply to positive UTC offsets.

However, when time fraction is supplied (with midnight) it yields expected results:

// Timezone set to -6
// when provided with time fraction (and without explicit time zone) it is constructed properly
new Date("2021-03-31T00:00:00")  // is in fact interpreted as "Wed Mar 31 2021 00:00:00 GMT-0600 (Central Standard Time)"

I believe that Date only values should be interpreted strictly as local date regardless of currently used time zone offset. The following fix could be used to provide such a behaviour:

else if (mapperType.match(/^(Date)$/ig) !== null) {
      payload = new Date(responseBody + "T00:00:00");
}
else if (mapperType.match(/^(DateTime|DateTimeRfc1123)$/ig) !== null) {
      payload = new Date(responseBody);
}

The very same behaviour is actually also present in the newer version of azure-sdk-for-js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions