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: avoid applying map property getters when saving #13704

Merged
merged 3 commits into from Aug 8, 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
16 changes: 15 additions & 1 deletion lib/utils.js
Expand Up @@ -630,9 +630,23 @@ function _populateObj(obj) {
*/

exports.getValue = function(path, obj, map) {
return mpath.get(path, obj, '_doc', map);
return mpath.get(path, obj, getValueLookup, map);
};

/*!
* ignore
*/

const mapGetterOptions = Object.freeze({ getters: false });

function getValueLookup(obj, part) {
const _from = obj && obj._doc ? obj._doc : obj;
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
obj = _from instanceof Map ?
_from.get(part, mapGetterOptions) :
_from[part];
return obj;
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets the value of `obj` at the given `path`.
*
Expand Down
31 changes: 31 additions & 0 deletions test/schema.uuid.test.js
Expand Up @@ -172,6 +172,37 @@ describe('SchemaUUID', function() {
assert.equal(_id, uuid.toString());
});

it('avoids converting maps of uuids to strings (gh-13657)', async function() {
const schema = new mongoose.Schema(
{
doc_map: {
type: mongoose.Schema.Types.Map,
of: mongoose.Schema.Types.UUID
}
}
);
db.deleteModel(/Test/);
const Test = db.model('Test', schema);
await Test.deleteMany({});

const user = new Test({
doc_map: new Map([
['role_1', new mongoose.Types.UUID()],
['role_2', new mongoose.Types.UUID()]
])
});

await user.save();

user.doc_map.set('role_1', new mongoose.Types.UUID());
await user.save();

const exists = await Test.findOne({ 'doc_map.role_1': { $type: 'binData' } });
assert.ok(exists);

assert.equal(typeof user.get('doc_map.role_1'), 'string');
});

// the following are TODOs based on SchemaUUID.prototype.$conditionalHandlers which are not tested yet
it('should work with $bits* operators');
it('should work with $all operator');
Expand Down