-
Notifications
You must be signed in to change notification settings - Fork 260
feat(NODE-7328): Add ignoreUndefined option to EJSON serialization #853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(NODE-7328): Add ignoreUndefined option to EJSON serialization #853
Conversation
- Introduced `ignoreUndefined` option to `EJSON.stringify` to omit undefined values from the output. - Updated tests to cover various scenarios for the new option, including nested objects and arrays. - Ensured compatibility with existing options like `relaxed` and `replacer` functions.
baileympearson
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ShtibsDev - thanks for this contribution! We agree that this makes sense.
I left a few small comments.
| * Omits undefined values from the output instead of converting them to null | ||
| * @defaultValue `false` | ||
| */ | ||
| ignoreUndefined?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option is only relevant to serialization, but EJSONOptions are shared for both serialization and deserialization.
What would you think about creating a separate interface for serialization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separated the types. Caused a little more changes than I initially intended to but If it's fine with you I'm glad to help :)
| expect(result).to.deep.equal({ a: 1 }); | ||
| }); | ||
|
|
||
| describe('ignoreUndefined option', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes you've made implicitly add support for ignoreUndefined to our EJSON.serialize function too. Could you add a set of tests like these tests for the EJSON.serialize function as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baileympearson Added tests for EJSON.serialize
Introduce EJSONSerializeOptions and EJSONParseOptions to distinguish between serialization-only options (ignoreUndefined) and parse-only options (useBigInt64). EJSONOptions remains as the intersection for backward compatibility. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
ignoreUndefinedoption toEJSON.stringifyto omit undefined values from the output.relaxedandreplacerfunctions.Description
Introduced
ignoreUndefinedoption toEJSON.stringifyto omit undefined values from the output.Summary of Changes
ignoreUndefinedoption toEJSON.stringifyto omit undefined values from the output.Notes for Reviewers
Currently, the
EJSON.stringifycan't emulate mongo's behavior in caseignoreUndefinedis set in theMongoClientconstructor.By adding this option, the
EJSON.stringifybecomes more flexible and allows this behavior.What is the motivation for this change?
Codebases using MongoClient's ignoreUndefined currently have no way to stringify objects with the same behaviour without manually removing undefined properties via a recursive function.
This new option will allow stringifying object while both keeping the EJSON behavior and the undefined omitting behaviour.
Double check the following
npm run check:lint)type(NODE-xxxx)[!]: descriptionfeat(NODE-1234)!: rewriting everything in coffeescript