Skip to content

Commit

Permalink
Merge branch 'master' into 5.10
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 1, 2020
2 parents 655114b + 0d60630 commit ab0a38b
Show file tree
Hide file tree
Showing 34 changed files with 80 additions and 38 deletions.
9 changes: 9 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
5.9.21 / 2020-07-01
===================
* fix: propagate `typeKey` option to implicitly created schemas from `typePojoToMixed` #9185 [joaoritter](https://github.com/joaoritter)
* fix(populate): handle embedded discriminator `refPath` with multiple documents #9153
* fix(populate): handle deselected foreign field with `perDocumentLimit` and multiple documents #9175
* fix(document): disallow `transform` functions that return promises #9176 #9163 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
* fix(document): use strict equality when checking mixed paths for modifications #9165
* docs: add target="_blank" to all edit links #9058

5.9.20 / 2020-06-22
===================
* fix(populate): handle populating primitive array under document array discriminator #9148
Expand Down
2 changes: 1 addition & 1 deletion docs/acquit.pug
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends layout

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/api.pug
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ block content
each item in docs
hr.separate-api
div.item-header-wrap
<a class="edit-docs-link" href="#{item.editLink}">
<a class="edit-docs-link" href="#{item.editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>
h2(id=item.name, class="item-header")
Expand Down
2 changes: 1 addition & 1 deletion docs/api_split.pug
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ append style
}

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>
h1 #{name}
Expand Down
2 changes: 1 addition & 1 deletion docs/async-await.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/browser.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/built-with-mongoose.pug
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends layout

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/compatibility.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/connections.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/deprecations.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/documents.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/faq.pug
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ block append style
}

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/further_reading.pug
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ append style
}

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/geojson.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/guide.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/guides.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/jest.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/lambda.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/middleware.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/migration.pug
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ block append style
}

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/models.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/plugins.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/populate.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/queries.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/schematypes.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/subdocs.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/transactions.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ block append style
script(type="text/javascript" src="/docs/js/native.js")

block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>

Expand Down
6 changes: 0 additions & 6 deletions index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,6 @@ html(lang='en')
<a href="https://www.bonus.com.de/freispiele">
<img class="sponsor" src="https://images.opencollective.com/bonusfinder-deutschland/646169e/logo.png" style="height: 100px">
</a>
<a href="https://www.monetta.pl/">
<img class="sponsor" src="https://images.opencollective.com/monetta/7d65ae6/logo.png" style="height: 100px">
</a>
<a href="https://www.minitool.com/">
<img class="sponsor" src="https://images.opencollective.com/minitool-solution-ltd/1519aa6/logo.png" style="height: 100px">
</a>
Expand All @@ -277,9 +274,6 @@ html(lang='en')
<a href="https://searchpromocodes.com/">
<img class="sponsor" src="https://images.opencollective.com/searchpromocodes/68ae5dd/logo/256.png" style="height: 100px">
</a>
<a href="https://unscramblex.com/">
<img class="sponsor" src="https://images.opencollective.com/unscramblex/87d6a68/logo/256.png" style="height: 100px">
</a>
<a href="https://www.nettikasinot.org/">
<img class="sponsor" src="https://images.opencollective.com/nettikasinot-org/bbd887f/logo/256.png" style="height: 100px">
</a>
Expand Down
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 @@ -3080,7 +3081,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 @@ -3419,13 +3420,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 @@ -3435,6 +3440,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;
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ Schema.prototype.add = function add(obj, prefix) {
// Propage `typePojoToMixed` to implicitly created schemas
const opts = { typePojoToMixed: false };
const _schema = new Schema(obj[key][this.options.typeKey], opts);
const schemaWrappedPath = Object.assign({}, obj[key], { type: _schema });
const schemaWrappedPath = Object.assign({}, obj[key], { [this.options.typeKey]: _schema });
this.path(prefix + key, schemaWrappedPath);
} else {
// Either the type is non-POJO or we interpret it as Mixed anyway
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "5.9.20",
"version": "5.9.21",
"author": "Guillermo Rauch <guillermo@learnboost.com>",
"keywords": [
"mongodb",
Expand Down
22 changes: 22 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9022,6 +9022,28 @@ describe('document', function() {
assert.ok(!doc.testSingleNested.prop);
});

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();
}, /must be synchronous/);

assert.throws(function() {
user.toObject();
}, /must be synchronous/);
});

it('uses strict equality when checking mixed paths for modifications (gh-9165)', function() {
const schema = Schema({ obj: {} });
const Model = db.model('gh9165', schema);
Expand Down
2 changes: 1 addition & 1 deletion website.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ append style
p { line-height: 1.5em }
block content
<a class="edit-docs-link" href="#{editLink}">
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>
:markdown
Expand Down

0 comments on commit ab0a38b

Please sign in to comment.