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(schema): avoid propagating toObject.transform and toJSON.transform option to implicitly created schemas #13634

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,10 @@ Schema.prototype.add = function add(obj, prefix) {
childSchemaOptions.strict = this._userProvidedOptions.strict;
}
if (this._userProvidedOptions.toObject != null) {
childSchemaOptions.toObject = this._userProvidedOptions.toObject;
childSchemaOptions.toObject = utils.omit(this._userProvidedOptions.toObject, ['transform']);
}
if (this._userProvidedOptions.toJSON != null) {
childSchemaOptions.toJSON = this._userProvidedOptions.toJSON;
childSchemaOptions.toJSON = utils.omit(this._userProvidedOptions.toJSON, ['transform']);
}

const _schema = new Schema(_typeDef, childSchemaOptions);
Expand Down Expand Up @@ -1346,10 +1346,10 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
childSchemaOptions.strictQuery = options.strictQuery;
}
if (options.hasOwnProperty('toObject')) {
childSchemaOptions.toObject = options.toObject;
childSchemaOptions.toObject = utils.omit(options.toObject, ['transform']);
}
if (options.hasOwnProperty('toJSON')) {
childSchemaOptions.toJSON = options.toJSON;
childSchemaOptions.toJSON = utils.omit(options.toJSON, ['transform']);
}

if (this._userProvidedOptions.hasOwnProperty('_id')) {
Expand Down
26 changes: 22 additions & 4 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,21 @@
assert.strictEqual(myModel.toObject().foo, void 0);
});

it('should propogate toObject to implicitly created schemas gh-13325', async function() {
it('should propogate toObject to implicitly created schemas (gh-13599) (gh-13325)', async function() {
let transformCalls = [];

Check warning on line 797 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'transformCalls' is never reassigned. Use 'const' instead

Check warning on line 797 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'transformCalls' is never reassigned. Use 'const' instead
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
const userSchema = Schema({
firstName: String,
company: {
type: { companyId: { type: Schema.Types.ObjectId }, companyName: String }
}
}, {
toObject: { virtuals: true }
toObject: {
virtuals: true,
transform(doc, ret) {
transformCalls.push(doc);
return ret;
}
}
});

userSchema.virtual('company.details').get(() => 42);
Expand All @@ -809,6 +816,8 @@
const user = new User({ firstName: 'test', company: { companyName: 'foo' } });
const obj = user.toObject();
assert.strictEqual(obj.company.details, 42);
assert.equal(transformCalls.length, 1);
assert.strictEqual(transformCalls[0], user);
});
});

Expand Down Expand Up @@ -996,15 +1005,22 @@
assert.equal(foundAlicJson.friends, undefined);
assert.equal(foundAlicJson.name, 'Alic');
});
it('should propogate toJSON to implicitly created schemas gh-13325', async function() {
it('should propogate toJSON to implicitly created schemas (gh-13599) (gh-13325)', async function() {
let transformCalls = [];

Check warning on line 1009 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'transformCalls' is never reassigned. Use 'const' instead

Check warning on line 1009 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'transformCalls' is never reassigned. Use 'const' instead
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
const userSchema = Schema({
firstName: String,
company: {
type: { companyId: { type: Schema.Types.ObjectId }, companyName: String }
}
}, {
id: false,
toJSON: { virtuals: true }
toJSON: {
virtuals: true,
transform(doc, ret) {
transformCalls.push(doc);
return ret;
}
}
});

userSchema.virtual('company.details').get(() => 'foo');
Expand All @@ -1016,6 +1032,8 @@
});
const obj = doc.toJSON();
assert.strictEqual(obj.company.details, 'foo');
assert.equal(transformCalls.length, 1);
assert.strictEqual(transformCalls[0], doc);
});
});

Expand Down