Skip to content

fix: preserve Date values in optimistic updates#11271

Open
louzhedong wants to merge 2 commits into
marmelab:masterfrom
louzhedong:fix/remove-undefined-preserve-dates
Open

fix: preserve Date values in optimistic updates#11271
louzhedong wants to merge 2 commits into
marmelab:masterfrom
louzhedong:fix/remove-undefined-preserve-dates

Conversation

@louzhedong

Copy link
Copy Markdown

Fixes #10478.

Summary

useUpdate and useUpdateMany previously used JSON.parse(JSON.stringify(...)) to remove undefined values from optimistic payloads. That also converted Date values to strings, which caused optimistic cache updates to lose date types.

This change replaces that serialization step with a recursive removeUndefined helper that:

  • removes undefined values from plain objects
  • preserves Date instances and other non-plain values
  • keeps the existing optimistic update behavior intact

Tests

  • yarn.cmd test-unit packages/ra-core/src/util/removeUndefined.spec.ts packages/ra-core/src/dataProvider/useUpdate.spec.tsx packages/ra-core/src/dataProvider/useUpdateMany.spec.tsx --runInBand --watch=false --testNamePattern "preserves Date values in the cache|does not erase values if the payload contains undefined values|removeUndefined"
  • yarn.cmd workspace ra-core build

Notes

I added a regression test that verifies optimistic cache updates keep Date values as Date instances, even when the payload also contains undefined fields.

@louzhedong

Copy link
Copy Markdown
Author

Quick follow-up: I re-checked the branch locally and the targeted tests are still green on my side. The only failing GitHub checks are the Vercel deployment authorizations, which look external to the code change. If you'd like, I can re-run or narrow anything further once there's a maintainer review comment.

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.

Unexpected conversion of Date to string in optimistic updates in useUpdate and Forms

1 participant