-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Fix: cast id parameter based on schema _id type in DocumentArray.id() #15725
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: cast id parameter based on schema _id type in DocumentArray.id() #15725
Conversation
Fixes Automattic#15724 The id() method now properly casts the id parameter based on the schema's _id type instead of always attempting to cast to ObjectId. This fixes issues when using custom _id types like String, Number, or Object.
vkarpov15
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.
Good overall direction, just have a few suggestions
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.
Pull Request Overview
This PR implements schema-based casting for the id() method in the DocumentArray type, addressing a TODO comment that was previously in the code. The implementation now attempts to cast the input id parameter using the schema's _id field type before performing comparisons.
Key changes:
- Adds schema-aware casting of the input
idparameter using the_idfield's schema type - Refactors the comparison logic to use the casted id value for more accurate matching
- Removes the TODO comment indicating this feature has been implemented
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Pull Request Overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } else if (castedId == null && (id == _id || utils.deepEqual(id, _id))) { | ||
| return val; |
Copilot
AI
Nov 8, 2025
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.
[nitpick] The fallback condition else if (castedId == null && (id == _id || utils.deepEqual(id, _id))) could potentially match when casting failed but the original id happens to match. This might give false positives if the intent is that casting failure should prevent matches.
Consider whether this fallback is necessary, or if a failed cast should always result in no match. If the fallback is intentional, a comment explaining why would be helpful.
| } else if (castedId == null && (id == _id || utils.deepEqual(id, _id))) { | |
| return val; |
| } else if (castedId != null && !isBsonType(castedId, 'ObjectId') && !isBsonType(_id, 'ObjectId')) { | ||
| if (castedId == _id || utils.deepEqual(castedId, _id)) { | ||
| return val; | ||
| } | ||
| } else if (casted == _id) { | ||
| } else if (castedId != null && isBsonType(castedId, 'ObjectId') && isBsonType(_id, 'ObjectId')) { | ||
| if (castedId.toString() === _id.toString()) { | ||
| return val; | ||
| } | ||
| } else if (castedId != null && castedId == _id) { | ||
| return val; | ||
| } else if (castedId == null && (id == _id || utils.deepEqual(id, _id))) { | ||
| return val; | ||
| } |
Copilot
AI
Nov 8, 2025
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.
[nitpick] The comparison logic starting at line 170 handles multiple scenarios but lacks explanatory comments. Consider adding brief comments for each condition to improve maintainability:
// Both castedId and _id are non-ObjectId types (e.g., string, number, object)
} else if (castedId != null && !isBsonType(castedId, 'ObjectId') && !isBsonType(_id, 'ObjectId')) {
if (castedId == _id || utils.deepEqual(castedId, _id)) {
return val;
}
// Both castedId and _id are ObjectIds - compare as strings
} else if (castedId != null && isBsonType(castedId, 'ObjectId') && isBsonType(_id, 'ObjectId')) {
if (castedId.toString() === _id.toString()) {
return val;
}
// Mixed types or other valid castedId scenarios
} else if (castedId != null && castedId == _id) {
return val;
// Fallback to original id when casting failed
} else if (castedId == null && (id == _id || utils.deepEqual(id, _id))) {
return val;
}Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
vkarpov15
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.
LGTM, I'll merge this into a separate branch so I can do some cleanup but otherwise I will merge this into 8.20.
Casting for DocumentArray#id() re: #1575
Closes #15724
Summary
The
DocumentArray.id()method was always attempting to cast theidparameter to ObjectId, which caused issues when using custom_idtypes like String, Number, or Object. This fix implements schema-based casting by:_idschema type from the document array schemaidparameter to match that type before comparisonThis resolves the TODO comment at line 124 and ensures the method works correctly with all
_idtypes that Mongoose supports.Example demonstrating the fix: