Skip to content

Commit 6987ba8

Browse files
committed
fix(document): make nested doc keys not enumerable again
Fix #5078
1 parent 25c350f commit 6987ba8

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

lib/document.js

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,25 +1767,44 @@ Document.prototype.$__dirty = function() {
17671767
*/
17681768

17691769
function compile(tree, proto, prefix, options) {
1770-
var keys = Object.keys(tree),
1771-
i = keys.length,
1772-
limb,
1773-
key;
1770+
var keys = Object.keys(tree);
1771+
var i = keys.length;
1772+
var len = keys.length;
1773+
var limb;
1774+
var key;
17741775

1775-
while (i--) {
1776-
key = keys[i];
1777-
limb = tree[key];
1778-
1779-
defineKey(key,
1780-
((utils.getFunctionName(limb.constructor) === 'Object'
1781-
&& Object.keys(limb).length)
1782-
&& (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type))
1783-
? limb
1784-
: null)
1785-
, proto
1786-
, prefix
1787-
, keys
1788-
, options);
1776+
if (options.retainKeyOrder) {
1777+
for (i = 0; i < len; ++i) {
1778+
key = keys[i];
1779+
limb = tree[key];
1780+
1781+
defineKey(key,
1782+
((utils.getFunctionName(limb.constructor) === 'Object'
1783+
&& Object.keys(limb).length)
1784+
&& (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type))
1785+
? limb
1786+
: null)
1787+
, proto
1788+
, prefix
1789+
, keys
1790+
, options);
1791+
}
1792+
} else {
1793+
while (i--) {
1794+
key = keys[i];
1795+
limb = tree[key];
1796+
1797+
defineKey(key,
1798+
((utils.getFunctionName(limb.constructor) === 'Object'
1799+
&& Object.keys(limb).length)
1800+
&& (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type))
1801+
? limb
1802+
: null)
1803+
, proto
1804+
, prefix
1805+
, keys
1806+
, options);
1807+
}
17891808
}
17901809
}
17911810

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

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

18021821
return result;
@@ -1844,7 +1863,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
18441863
}
18451864

18461865
Object.defineProperty(nested, 'toObject', {
1847-
enumerable: true,
1866+
enumerable: false,
18481867
configurable: true,
18491868
writable: false,
18501869
value: function() {
@@ -1853,7 +1872,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
18531872
});
18541873

18551874
Object.defineProperty(nested, 'toJSON', {
1856-
enumerable: true,
1875+
enumerable: false,
18571876
configurable: true,
18581877
writable: false,
18591878
value: function() {
@@ -1862,7 +1881,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
18621881
});
18631882

18641883
Object.defineProperty(nested, '$__isNested', {
1865-
enumerable: true,
1884+
enumerable: false,
18661885
configurable: true,
18671886
writable: false,
18681887
value: true

test/document.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,6 +4037,33 @@ describe('document', function() {
40374037
done();
40384038
});
40394039

4040+
it('iterating through nested doc keys (gh-5078)', function(done) {
4041+
var schema = new Schema({
4042+
nested: {
4043+
test1: String,
4044+
test2: String
4045+
}
4046+
}, { retainKeyOrder: true });
4047+
4048+
schema.virtual('tests').get(function() {
4049+
return _.map(this.nested, function(v, key) {
4050+
return v;
4051+
})
4052+
});
4053+
4054+
var M = db.model('gh5078', schema);
4055+
4056+
var doc = new M({ nested: { test1: 'a', test2: 'b' } });
4057+
4058+
assert.deepEqual(doc.toObject({ virtuals: true }).tests, ['a', 'b']);
4059+
4060+
// Should not throw
4061+
require('util').inspect(doc);
4062+
JSON.stringify(doc);
4063+
4064+
done();
4065+
});
4066+
40404067
it('JSON.stringify nested errors (gh-5208)', function(done) {
40414068
var AdditionalContactSchema = new Schema({
40424069
contactName: {

0 commit comments

Comments
 (0)