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
BSONError thrown when calling _id.toString() after using R.clone after mongoose bump #14353
Comments
I am also able to reproduce with it.only('modification of test "$clone() (gh-11849)"', async function() {
const schema = new mongoose.Schema({
subDocArray: [{
name: {
type: String,
validate: {
validator: (v) => v !== 'Invalid'
}
}
}]
});
const Test = db.model('Test', schema);
const item = await Test.create({ subDocArray: [{ name: 'Test' }] });
const doc = await Test.findById(item._id);
const clonedDoc = doc.$clone();
// This assertion fails
assert.deepEqual(clonedDoc.subDocArray[0], doc.subDocArray[0]);
}); |
const mongoose = require('mongoose');
const assert = require('assert');
const testSchema = new mongoose.Schema({
subDocArray: [{
name: {
type: String,
validate: {
validator: (v) => v !== 'Invalid'
}
}
}]
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({ subDocArray: [{ name: 'Test' }] });
const doc = await Test.findOne();
const cloneTrooper = doc.$clone();
assert.deepEqual(cloneTrooper.subDocArray[0], doc.subDocArray[0]);
console.log('done');
}
run(); |
I repro-ed OP's post using the following:
@gitkrakel your issue looks unrelated to OP's, but we'll take a look at that as well |
@Axosoft-Ashley the I'll do some more digging to see if there's a way we can reconcile these two. But given that for (var key of Object.keys(value).concat(Object.getOwnPropertySymbols(value))) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
copiedValue[key] = deep ? _clone(value[key], true, map) : value[key];
}
} |
Actually, it looks like bson@6.4 (just released today) fixes this issue, so const R = require('ramda');
const mongoose = require('mongoose');
const OrganizationModel = mongoose.model('Organization', mongoose.Schema({ members: [{ user: 'ObjectId' }] }));
const UserModel = mongoose.model('User', mongoose.Schema({ members: ['ObjectId'] }));
void async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
let foundOrgModel = await OrganizationModel.findOne({});
if (!foundOrgModel) {
// create user
const user = await UserModel.create({})
// create org
foundOrgModel = await OrganizationModel.create({ members: [{ user: user._id }] })
}
foundOrgModel = await OrganizationModel.findOne({})
const members = R.clone(foundOrgModel.members);
console.log(members[0]._id.toString()) // crashes
}(); Related issue here: mongodb/js-bson#643. So @Axosoft-Ashley please re-run |
@vkarpov15 I tested in my playground and it did appear to fix the issue with 6.4.0 of bson! Thank you 😄 |
fix(document): make `$clone` avoid converting subdocs into POJOs
Prerequisites
Mongoose version
8.1.2
Node.js version
18
MongoDB server version
6.0
Typescript version (if applicable)
No response
Description
After bumping from v5.13.20 to 8.1.2, when R.clone() is called on an array of mongoose documents a BSON error is thrown, this did not happen on the older version of mongoose.
throw new BSONError("Cannot create Buffer from ${String(potentialBuffer)}");
Ramda version ^0.29.1 was used for both old and new mongoose test
Steps to Reproduce
schema setup
crash code:
Expected Behavior
it should not throw but instead just log the id as a string like it did in v5.13.20 of mongoose
The text was updated successfully, but these errors were encountered: