Skip to content
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 findByIdAndUpdate behavior with undefined id #11079

Merged
merged 6 commits into from Dec 24, 2021

Conversation

gramliu
Copy link
Contributor

@gramliu gramliu commented Dec 11, 2021

Summary

findByIdAndUpdate should not work with an undefined id, if an update is specified. Resolves #10964

Examples

I added a test to demonstrate this change.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

I think this PR is helpful, but causes some test failures. We can merge this when those get sorted out

lib/model.js Outdated
@@ -2676,6 +2680,9 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
throw new TypeError(msg);
}
return this.findOneAndUpdate({ _id: id }, undefined);
} else if (id === undefined) {
const msg = 'Model.findByIdAndUpdate(): id cannot be undefined if update is specified.\n';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why \n?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I was just following the format of the preceding error messages. I went ahead and removed it

lib/model.js Outdated
@@ -2676,6 +2680,9 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
throw new TypeError(msg);
}
return this.findOneAndUpdate({ _id: id }, undefined);
} else if (id === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Our tests rely on Model.findByIdAndUpdate() with no arguments working, so can we make this else if (arguments.length > 0 && id === undefined)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for bringing this up. I forgot to consider that. I went ahead and added this to the condition

@gramliu
Copy link
Contributor Author

gramliu commented Dec 12, 2021

@vkarpov15 I pushed some changes that might resolve the failing tests. For the function itself, I added the check for undefined id only if the length of arguments was greater than 1. Moreover, I updated the test I wrote to properly check that the error is thrown. It turns out that my original test was wrong

@vkarpov15 vkarpov15 added this to the 6.1.4 milestone Dec 24, 2021
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Needed to patch up the tests but LGTM overall 👍

@vkarpov15 vkarpov15 merged commit 6d41296 into Automattic:master Dec 24, 2021
vkarpov15 added a commit that referenced this pull request Jan 6, 2022
@vkarpov15
Copy link
Collaborator

Unfortunately I have to revert this change - it is backwards breaking as indicated in #11149. We'll consider this for our next major release.

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.

findByIdAndUpdate should not work with undefined id
2 participants