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: hasAtomicOperator check respects toBSON transformation #2696

Merged
merged 4 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,11 @@ function hasAtomicOperators(doc) {
return doc.reduce((err, u) => err || hasAtomicOperators(u), null);
}

const keys = Object.keys(doc);
return keys.length > 0 && keys[0][0] === '$';
return (
Object.keys(typeof doc.toBSON !== 'function' ? doc : doc.toBSON())
.map(k => k[0])
.indexOf('$') >= 0
);
}

module.exports = {
Expand Down
63 changes: 63 additions & 0 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1662,4 +1662,67 @@ describe('Bulk', function() {
);
});
});

it('should enforce no atomic operators', function() {
const client = this.configuration.newClient();
return client
.connect()
.then(() => {
const collection = client.db().collection('noAtomicOp');
return collection
.drop()
.catch(ignoreNsNotFound)
.then(() => collection);
})
.then(collection => {
return collection.insertMany([{ a: 1 }, { a: 1 }, { a: 1 }]).then(() => collection);
})
.then(collection => {
try {
return collection.replaceOne({ a: 1 }, { $atomic: 1 });
} catch (err) {
expect(err).to.be.instanceOf(
TypeError,
'Replacement document must not use atomic operators'
);
}
})
.finally(() => {
return client.close();
});
});

it('should respect toBSON conversion when checking for atomic operators', function() {
const client = this.configuration.newClient();
return client
.connect()
.then(() => {
const collection = client.db().collection('noAtomicOp');
return collection
.drop()
.catch(ignoreNsNotFound)
.then(() => collection);
})
.then(collection => {
return collection.insertMany([{ a: 1 }, { a: 1 }, { a: 1 }]).then(() => collection);
})
.then(collection => {
try {
return collection.replaceOne(
{ a: 1 },
{
$atomic: 1,
toBSON() {
return { atomic: this.$atomic };
}
}
);
} catch (err) {
expect.fail(); // shouldn't throw any error
}
})
.finally(() => {
return client.close();
});
});
});
14 changes: 14 additions & 0 deletions test/unit/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const eachAsync = require('../../lib/core/utils').eachAsync;
const makeInterruptableAsyncInterval = require('../../lib/utils').makeInterruptableAsyncInterval;
const now = require('../../lib/utils').now;
const hasAtomicOperators = require('../../lib/utils').hasAtomicOperators;
const expect = require('chai').expect;
const sinon = require('sinon');

Expand Down Expand Up @@ -163,4 +164,17 @@ describe('utils', function() {
this.clock.tick(250);
});
});

it('should assert hasAtomicOperators and respect toBSON conversion', function() {
expect(hasAtomicOperators({ $key: 2.3 })).to.be.true;
expect(hasAtomicOperators({ nonAtomic: 1, $key: 2.3 })).to.be.true;
expect(
hasAtomicOperators({
$key: 2.3,
toBSON() {
return { key: this.$key };
}
})
).to.be.false;
});
});