-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Bug: time attribute is always a string, yet is type casted to string | Date #326
Comments
@moltar - good catch. All of this conversion with the When we refactored this repository into TypeScript, there was an effort to create the I think we need to consider the fact that the @cloudevents/sdk-javascript-maintainers any thoughts on this? |
If we are adding in a |
@lholmquist You are probably correct, but I worry the changes would be bigger than that for TypeScript users, since we will be potentially changing function type signatures. |
I've been thinking about this, poking around some of the code, and reading some TypeScript issues. At first, I thought that the easiest and most straightforward way to handle this would be to just change the getters and setters so that the setter takes https://github.com/cloudevents/sdk-javascript/blob/main/src/event/cloudevent.ts#L126-L132 However, I ran into the problem noted in this issue, that the types for the getter and setter must match. That issue was originally created 5 years ago, and it's been discussed a lot. This is not going to change. So... we have a couple of choices.
I'm leaning towards 3 but would like to hear other's thoughts. /cc @cloudevents/sdk-javascript-maintainers |
I think we probably don't need to convert/touch the The JSON format is only needed for transport, not internal representation. Javascript natively maps types to JSON like JS will always JSON stringify values. The ISO string may lead to weird behavior. We should probably just not touch the CE#time imo. It'll always be compliant to the spec. |
I generally agree, but from MDN.
So there is that to think about.
It's not as cut and dried as that, unfortunately. The let d = new Date()
d.toString() // 'Fri Aug 28 2020 13:27:13 GMT-0400 (Eastern Daylight Time)'
d.toISOString() // '2020-08-28T17:27:13.043Z' This is a problem with accepting timestamps as a string because the user could do something like this. I'm not saying it's smart, but they could do it and break compatibility unless we manipulate the incoming string. const date = new Date();
const ce = new CloudEvent({
source: '/',
type: 'example',
time: date.toString()
}); This is why we have the special logic specifically for the Fortunately, Given all of the above, I think we should probably declare that only a |
All good points. The main points I want to communicate:
|
Previously, the event's `time` property could be either a string or a date. this commit modifies that to ensure that the object can only be created with a timestamp in string format. As long as the string is a valid date, that can be parsed by `new Date(Date.parse(str))` then whenever the event is serialized as JSON, the `time` attribute will be formatted as per RFC 3339. Fixes: cloudevents#326 Signed-off-by: Lance Ball <lball@redhat.com>
Previously, the event's `time` property could be either a string or a date. this commit modifies that to ensure that the object can only be created with a timestamp in string format. As long as the string is a valid date, that can be parsed by `new Date(Date.parse(str))` then whenever the event is serialized as JSON, the `time` attribute will be formatted as per RFC 3339. Fixes: #326 Signed-off-by: Lance Ball <lball@redhat.com>
Thanks! :) |
It appears that
time
constructor attribute value, which can beDate
is always converted to a string:sdk-javascript/src/event/cloudevent.ts
Lines 91 to 95 in 73f0bec
The setter also converts to a string:
sdk-javascript/src/event/cloudevent.ts
Lines 130 to 132 in 73f0bec
Yet, the value of the getter is type casted to
string | Date
. This is actually confusing and leads to buggy behavior.This led me to a wrong conclusion, that if I pass a
Date
it will remain aDate
.sdk-javascript/src/event/cloudevent.ts
Line 126 in 73f0bec
The text was updated successfully, but these errors were encountered: