Skip to content

Commit

Permalink
Merge pull request #13634 from Automattic/vkarpov15/gh-13599
Browse files Browse the repository at this point in the history
fix(schema): avoid propagating `toObject.transform` and `toJSON.transform` option to implicitly created schemas
  • Loading branch information
vkarpov15 committed Jul 20, 2023
2 parents d246223 + 2e34c01 commit ef95e02
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
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 @@ describe('document', function() {
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() {
const transformCalls = [];
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 @@ describe('document', function() {
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 @@ describe('document', function() {
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() {
const transformCalls = [];
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 @@ describe('document', function() {
});
const obj = doc.toJSON();
assert.strictEqual(obj.company.details, 'foo');
assert.equal(transformCalls.length, 1);
assert.strictEqual(transformCalls[0], doc);
});
});

Expand Down

0 comments on commit ef95e02

Please sign in to comment.