Skip to content

Conversation

@alexandre-abrioux
Copy link
Contributor

This PR fixes an issue I introduced in #6094

Using toISOString() on an invalid date throws a RangeError exception. We should test if the date is valid before comparing it with toISOString().

expect(compareObjects(new Date('2024-10-01T08:54:48.651Z'), new Date('2024-10-01T08:54:48.651Z'))).toBe(true);
expect(compareObjects(new Date('2024-10-01T08:54:48.651Z'), new Date('2024-10-01T08:54:48.676Z'))).toBe(false);
expect(compareObjects(new Date('2024-10-01T08:54:48.651Z'), new Date(NaN))).toBe(false);
expect(compareObjects(new Date(NaN), new Date(NaN))).toBe(true);
Copy link
Member

Choose a reason for hiding this comment

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

i would focus more on how this happened instead of supporting it on this low level helper. this feels like you are fixing it on wrong level.

@codecov
Copy link

codecov bot commented Oct 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.79%. Comparing base (d2052ab) to head (5d062bc).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #6120    +/-   ##
========================================
  Coverage   99.79%   99.79%            
========================================
  Files         264      264            
  Lines       18825    18870    +45     
  Branches     4685     4469   -216     
========================================
+ Hits        18786    18831    +45     
  Misses         39       39            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@B4nan
Copy link
Member

B4nan commented Oct 7, 2024

maybe we could revert those recent changes, since they were added because of how structuredClone works in combination with jest, and we no longer use structuredClone internally?

@alexandre-abrioux
Copy link
Contributor Author

alexandre-abrioux commented Oct 7, 2024

i would focus more on how this happened instead of supporting it on this low level helper. this feels like you are fixing it on wrong level.

@B4nan You were right! We should not fix this the way I did, or even not fix it at all.

I was initially trying to replicate the behavior of v6.3.10, but in the end I think current behavior is safer. I've added an integration test to document the use-case.

In v6.3.10, when updating a JSON property in MongoDB from a valid date to an invalid date, the date was simply erased and replaced by the Unix time 1970-01-01T00:00:00.000Z. This is IMO dangerous and unwanted. It is very easy to make such a mistake by instantiating a date with an undefined value in the constructor: new Date(undefined).

After more thought, I think it is best to throw an error and explain that this behavior is unsupported. There are no "invalid dates" in the BSON format, so I don't think we should handle that at all.

In the last commit I pushed on this PR, I changed the function to throw an exception when we encounter such a case. This does not "fix" anything but makes it explicit that there is an issue with the user code. Investigating this made me find an issue in the codebase I'm working on, so this is a good thing 🙏

@alexandre-abrioux
Copy link
Contributor Author

maybe we could revert those recent changes, since they were added because of how structuredClone works in combination with jest, and we no longer use structuredClone internally?

Yes probably! Let's settle on the desired behavior for updates with invalid dates, then with all the tests we've added recently, it should be safe to try 👌

@B4nan
Copy link
Member

B4nan commented Oct 7, 2024

I don't mind explicit exceptions as you did. Perf wise, I would rather store the getTime() values and compare those instead of using toISOString and comparing strings.

@alexandre-abrioux
Copy link
Contributor Author

Good point! done

@B4nan B4nan changed the title fix(core/utils): compare invalid dates fix(core): improve error handling for comparing invalid dates Oct 7, 2024
@B4nan B4nan merged commit 1aa940b into mikro-orm:master Oct 7, 2024
@alexandre-abrioux alexandre-abrioux deleted the invalid-dates branch October 7, 2024 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants