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(document): disallow transform functions that return promises #9176

Merged
merged 6 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 14 additions & 3 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const inspect = require('util').inspect;
const internalToObjectOptions = require('./options').internalToObjectOptions;
const mpath = require('mpath');
const utils = require('./utils');
const isPromise = require('./helpers/isPromise');

const clone = utils.clone;
const deepEqual = utils.deepEqual;
Expand Down Expand Up @@ -3075,7 +3076,7 @@ Document.prototype.$toObject = function(options, json) {
// we need to adjust options.transform to be the child schema's transform and
// not the parent schema's
if (transform) {
applySchemaTypeTransforms(this, ret, gettersOptions, options);
applySchemaTypeTransforms(this, ret);
}

if (transform === true || (schemaOptions.toObject && transform)) {
Expand Down Expand Up @@ -3414,13 +3415,17 @@ function applySchemaTypeTransforms(self, json) {
const schematype = schema.paths[path];
if (typeof schematype.options.transform === 'function') {
const val = self.get(path);
json[path] = schematype.options.transform.call(self, val);
const transformedValue = schematype.options.transform.call(self, val);
throwErrorIfPromise(path, transformedValue);
json[path] = transformedValue;
} else if (schematype.$embeddedSchemaType != null &&
typeof schematype.$embeddedSchemaType.options.transform === 'function') {
const vals = [].concat(self.get(path));
const transform = schematype.$embeddedSchemaType.options.transform;
for (let i = 0; i < vals.length; ++i) {
vals[i] = transform.call(self, vals[i]);
const transformedValue = transform.call(self, vals[i]);
vals[i] = transformedValue;
throwErrorIfPromise(path, transformedValue);
}

json[path] = vals;
Expand All @@ -3430,6 +3435,12 @@ function applySchemaTypeTransforms(self, json) {
return json;
}

function throwErrorIfPromise(path, transformedValue) {
if (isPromise(transformedValue)) {
throw new Error('`transform` function must be synchronous, but the transform on path `' + path + '` returned a promise.');
}
}

/**
* The return value of this method is used in calls to JSON.stringify(doc).
*
Expand Down
6 changes: 6 additions & 0 deletions lib/helpers/isPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
function isPromise(val) {
return !!val && (typeof val === 'object' || typeof val === 'function') && typeof val.then === 'function';
}

module.exports = isPromise;
22 changes: 22 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8975,4 +8975,26 @@ describe('document', function() {
const axl = new Person({ fullName: 'Axl Rose' });
assert.equal(axl.fullName, 'Axl Rose');
});

it('throws an error when `transform` returns a promise (gh-9163)', function() {
const userSchema = new Schema({
name: {
type: String,
transform: function() {
return new Promise(() => {});
}
}
});

const User = db.model('User', userSchema);

const user = new User({ name: 'Hafez' });
assert.throws(function() {
user.toJSON();
}, /`transform` option has to be synchronous, but is returning a promise on path `name`./);

assert.throws(function() {
user.toObject();
}, /`transform` option has to be synchronous, but is returning a promise on path `name`./);
});
});