-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
fix(document): fix ObjectId conversion for external schemas #11841
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When dealing with external schemas, i.e. a mongoose schema created inside another module, instanceOf may not work. This results in the value of ObjectIds in the schema being converted to strings when saving to the database.
This comment was marked as outdated.
This comment was marked as outdated.
Can you search the whole codebase for instanceof ObjectId, please? |
I didn't replace a few:
Same for |
AbdelrahmanHafez
approved these changes
May 28, 2022
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.
LGTM, thanks! 👍
LGTM! |
Uzlopak
approved these changes
May 28, 2022
noseworthy
added a commit
to noseworthy/mongoose
that referenced
this pull request
Jun 3, 2022
PR Automattic#11841 changed a whole bunch of checks for `instanceof ObjectId` to use the `isBsonType()` function instead. Unfortunately some were missed in the `./lib/schema/objectid.js` file. Convert these last couple checks to the new format. I grepped the code for `instanceof oid` to see if I got all the ones of this style and couldn't find anymore. This was found because my nested document with required ObjectIds started failing validation when I loaded the documents from JSON files in my tests using `bson.EJSON` after the release of 6.3.4 -> 6.3.5.
noseworthy
added a commit
to noseworthy/mongoose
that referenced
this pull request
Jun 3, 2022
PR Automattic#11841 changed a whole bunch of checks for `instanceof ObjectId` to use the `isBsonType()` function instead. Unfortunately some were missed in the `./lib/schema/objectid.js` file. Convert the required check for `objectid` to use both `instanceof` and `isBsonType()` to ensure that all ObjectIds are valid. This was found because my nested document with required ObjectIds started failing validation when I loaded the documents from JSON files in my tests using `bson.EJSON` after the release of 6.3.4 -> 6.3.5.
noseworthy
added a commit
to noseworthy/mongoose
that referenced
this pull request
Jun 3, 2022
PR Automattic#11841 changed a whole bunch of checks for `instanceof ObjectId` to use the `isBsonType()` function instead. Unfortunately some were missed in the `./lib/schema/objectid.js` file. Convert the remaining `instanceof` checks in `objectid.js` to use the `isBsonType()` check instead so that all `ObjectId`s created from any `bson` modules should validate/cast properly. This was found because my nested document with required ObjectIds started failing validation when I loaded the documents from JSON files in my tests using `bson.EJSON` after the release of 6.3.4 -> 6.3.5.
noseworthy
added a commit
to noseworthy/mongoose
that referenced
this pull request
Jun 3, 2022
PR Automattic#11841 changed a whole bunch of checks for `instanceof ObjectId` to use the `isBsonType()` function instead. Unfortunately some were missed in the `./lib/schema/objectid.js` file. Convert the remaining `instanceof` checks in `objectid.js` to use the `isBsonType()` check instead so that all `ObjectId`s created from any `bson` modules should validate/cast properly. This was found because my nested document with required ObjectIds started failing validation when I loaded the documents from JSON files in my tests using `bson.EJSON` after the release of 6.3.4 -> 6.3.5.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
When dealing with external schemas, i.e. a mongoose schema created inside another module,
instanceOf
may not work.This results in the value of ObjectIds in the schema being converted to strings when saving to the database.
Examples
I have a monorepo project with pnpm. One module
lib
contains a schema, which I use to create a model in my program.Both my program and the imported
lib
module have the samebson
,mongoose
andmongodb
versions.