Skip to content

Commit

Permalink
fix(cursor): transforms should only be applied once to documents
Browse files Browse the repository at this point in the history
A regression in merging the core cursor into the native codebase
resultied in a document transform (set with the `.map` helper) to
be applied to incoming documents twice.

NODE-2503
  • Loading branch information
mbroadst committed Mar 17, 2020
1 parent d7ac176 commit 704f30a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
9 changes: 0 additions & 9 deletions lib/core/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,6 @@ class CoreCursor extends Readable {
return this.push(this.cursorState.streamOptions.transform(result));
}

// If we provided a map function
if (
this.cursorState.transforms &&
typeof this.cursorState.transforms.doc === 'function' &&
result != null
) {
return this.push(this.cursorState.transforms.doc(result));
}

// Return the result
this.push(result);

Expand Down
7 changes: 1 addition & 6 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ class Cursor extends CoreCursor {
driver: true
});
}

return maybePromise(this, callback, cb => {
const cursor = this;
const items = [];
Expand Down Expand Up @@ -846,12 +847,6 @@ class Cursor extends CoreCursor {
// Get all buffered objects
if (cursor.bufferedCount() > 0) {
let docs = cursor.readBufferedDocuments(cursor.bufferedCount());

// Transform the doc if transform method added
if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') {
docs = docs.map(cursor.s.transforms.doc);
}

Array.prototype.push.apply(items, docs);
}

Expand Down
54 changes: 54 additions & 0 deletions test/functional/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4609,4 +4609,58 @@ describe('Cursor', function() {
});
});
});

describe('transforms', function() {
it('should correctly apply map transform to cursor as readable stream', function(done) {
const configuration = this.configuration;
const client = configuration.newClient();
client.connect(err => {
expect(err).to.not.exist;
this.defer(() => client.close());

const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x }));
const coll = client.db(configuration.db).collection('cursor_stream_mapping');
coll.insertMany(docs, err => {
expect(err).to.not.exist;

const bag = [];
const stream = coll
.find()
.project({ _id: 0, name: 1 })
.map(doc => ({ mapped: doc }))
.on('data', doc => bag.push(doc));

stream.on('error', done).on('end', () => {
expect(bag.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name })));
done();
});
});
});
});

it('should correctly apply map transform when converting cursor to array', function(done) {
const configuration = this.configuration;
const client = configuration.newClient();
client.connect(err => {
expect(err).to.not.exist;
this.defer(() => client.close());

const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x }));
const coll = client.db(configuration.db).collection('cursor_toArray_mapping');
coll.insertMany(docs, err => {
expect(err).to.not.exist;

coll
.find()
.project({ _id: 0, name: 1 })
.map(doc => ({ mapped: doc }))
.toArray((err, mappedDocs) => {
expect(err).to.not.exist;
expect(mappedDocs.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name })));
done();
});
});
});
});
});
});

0 comments on commit 704f30a

Please sign in to comment.