Skip to content

Commit

Permalink
fix(document): make nested doc keys not enumerable again
Browse files Browse the repository at this point in the history
Fix #5078
  • Loading branch information
vkarpov15 committed May 15, 2017
1 parent 25c350f commit 6987ba8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 22 deletions.
63 changes: 41 additions & 22 deletions lib/document.js
Expand Up @@ -1767,25 +1767,44 @@ Document.prototype.$__dirty = function() {
*/

function compile(tree, proto, prefix, options) {
var keys = Object.keys(tree),
i = keys.length,
limb,
key;
var keys = Object.keys(tree);
var i = keys.length;
var len = keys.length;
var limb;
var key;

while (i--) {
key = keys[i];
limb = tree[key];

defineKey(key,
((utils.getFunctionName(limb.constructor) === 'Object'
&& Object.keys(limb).length)
&& (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type))
? limb
: null)
, proto
, prefix
, keys
, options);
if (options.retainKeyOrder) {
for (i = 0; i < len; ++i) {
key = keys[i];
limb = tree[key];

defineKey(key,
((utils.getFunctionName(limb.constructor) === 'Object'
&& Object.keys(limb).length)
&& (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type))
? limb
: null)
, proto
, prefix
, keys
, options);
}
} else {
while (i--) {
key = keys[i];
limb = tree[key];

defineKey(key,
((utils.getFunctionName(limb.constructor) === 'Object'
&& Object.keys(limb).length)
&& (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type))
? limb
: null)
, proto
, prefix
, keys
, options);
}
}
}

Expand All @@ -1796,7 +1815,7 @@ function getOwnPropertyDescriptors(object) {

Object.getOwnPropertyNames(object).forEach(function(key) {
result[key] = Object.getOwnPropertyDescriptor(object, key);
result[key].enumerable = true;
result[key].enumerable = ['isNew', '$__', 'errors', '_doc'].indexOf(key) === -1;
});

return result;
Expand Down Expand Up @@ -1844,7 +1863,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
}

Object.defineProperty(nested, 'toObject', {
enumerable: true,
enumerable: false,
configurable: true,
writable: false,
value: function() {
Expand All @@ -1853,7 +1872,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
});

Object.defineProperty(nested, 'toJSON', {
enumerable: true,
enumerable: false,
configurable: true,
writable: false,
value: function() {
Expand All @@ -1862,7 +1881,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
});

Object.defineProperty(nested, '$__isNested', {
enumerable: true,
enumerable: false,
configurable: true,
writable: false,
value: true
Expand Down
27 changes: 27 additions & 0 deletions test/document.test.js
Expand Up @@ -4037,6 +4037,33 @@ describe('document', function() {
done();
});

it('iterating through nested doc keys (gh-5078)', function(done) {
var schema = new Schema({
nested: {
test1: String,
test2: String
}
}, { retainKeyOrder: true });

schema.virtual('tests').get(function() {
return _.map(this.nested, function(v, key) {
return v;
})
});

var M = db.model('gh5078', schema);

var doc = new M({ nested: { test1: 'a', test2: 'b' } });

assert.deepEqual(doc.toObject({ virtuals: true }).tests, ['a', 'b']);

// Should not throw
require('util').inspect(doc);
JSON.stringify(doc);

done();
});

it('JSON.stringify nested errors (gh-5208)', function(done) {
var AdditionalContactSchema = new Schema({
contactName: {
Expand Down

0 comments on commit 6987ba8

Please sign in to comment.