question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: time attribute is always a string, yet is type casted to string | Date

See original GitHub issue

It appears that time constructor attribute value, which can be Date is always converted to a string:

https://github.com/cloudevents/sdk-javascript/blob/73f0becc2b8e4f10ae40e23b77e4161d9b5ff611/src/event/cloudevent.ts#L91-L95

The setter also converts to a string:

https://github.com/cloudevents/sdk-javascript/blob/73f0becc2b8e4f10ae40e23b77e4161d9b5ff611/src/event/cloudevent.ts#L130-L132

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 a Date.

https://github.com/cloudevents/sdk-javascript/blob/73f0becc2b8e4f10ae40e23b77e4161d9b5ff611/src/event/cloudevent.ts#L126

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
lancecommented, Aug 25, 2020

@moltar - good catch. All of this conversion with the time attribute is done because the spec for JSON representation of an event specifically requires a string. But I think this is highlighting a deeper issue related to the JSON representation.

When we refactored this repository into TypeScript, there was an effort to create the CloudEvent class in such a way that simply doing console.log(event) would result in a JSON representation adhering to the spec. This breaks down in a few places and is why you see some attribute transformation as in this case.

I think we need to consider the fact that the CloudEvent class has to be explicitly transformed via toString() and JSON.stringify() in order to have a valid JSON representation of an event. We are working on some big API changes for the 4.0.0 version, and I think that this question should be addressed in 4.0. Resolving this, would result in a fix for what you highlight here, as well.

@cloudevents/sdk-javascript-maintainers any thoughts on this?

0reactions
moltarcommented, Sep 3, 2020

Thanks! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conversion failed when converting date and/or time from ...
Conversion failed when converting date and/or time from character string. The reason for this error is simply there is no such date (Feb-29)...
Read more >
Data Conversion - Error when trying to convert string to date
The Data Conversion component will work fine - but it only works when the string data is formatted properly, which is almost never...
Read more >
How to Handle the Incompatible Types Error in Java - Rollbar
The main method in Fig. 2(a) declares the variable date which holds the current date as a String in the yyyy-MM-dd format ...
Read more >
How to Convert a String to a Date In Swift - Cocoacasts
The DateFormatter is powerful, but it isn't magical. It cannot infer the date format of the string. That is something we need to...
Read more >
Java 8 - How to convert String to LocalDate - Mkyong.com
If the String is in ISO_LOCAL_DATE format, we can parse the String directly, no need conversion. JavaDateExample1.java. package com.mkyong.date; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found