Skip to content

Commit 281568e

Browse files
drew-grossflovilmart
authored andcommitted
Fixes #1417
* Fixes #1417 * Cleanup * Perf improvement * Hoist constant array * Improve tests
1 parent b433fb9 commit 281568e

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

spec/transform.spec.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// These tests are unit tests designed to only test transform.js.
2+
"use strict";
23

3-
var transform = require('../src/transform');
4+
let transform = require('../src/transform');
5+
let dd = require('deep-diff');
46

57
var dummySchema = {
68
data: {},
@@ -150,14 +152,30 @@ describe('untransformObject', () => {
150152
done();
151153
});
152154

153-
it('nested array', (done) => {
154-
var input = {arr: [{_testKey: 'testValue' }]};
155-
var output = transform.untransformObject(dummySchema, null, input);
156-
expect(Array.isArray(output.arr)).toEqual(true);
157-
expect(output.arr).toEqual([{ _testKey: 'testValue'}]);
158-
done();
159-
});
155+
it('nested array', (done) => {
156+
var input = {arr: [{_testKey: 'testValue' }]};
157+
var output = transform.untransformObject(dummySchema, null, input);
158+
expect(Array.isArray(output.arr)).toEqual(true);
159+
expect(output.arr).toEqual([{ _testKey: 'testValue'}]);
160+
done();
161+
});
160162

163+
it('untransforms objects containing nested special keys', done => {
164+
let input = {array: [{
165+
_id: "Test ID",
166+
_hashed_password: "I Don't know why you would name a key this, but if you do it should work",
167+
_tombstone: {
168+
_updated_at: "I'm sure people will nest keys like this",
169+
_acl: 7,
170+
_id: { someString: "str", someNumber: 7},
171+
regularKey: { moreContents: [1, 2, 3] },
172+
},
173+
regularKey: "some data",
174+
}]}
175+
let output = transform.untransformObject(dummySchema, null, input);
176+
expect(dd(output, input)).toEqual(undefined);
177+
done();
178+
});
161179
});
162180

163181
describe('transformKey', () => {

src/transform.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import log from './logger';
2+
import _ from 'lodash';
23
var mongodb = require('mongodb');
34
var Parse = require('parse/node').Parse;
45

@@ -149,8 +150,6 @@ export function transformKeyValue(schema, className, restKey, restValue, options
149150
throw 'There was a problem transforming an ACL.';
150151
}
151152

152-
153-
154153
// Handle arrays
155154
if (restValue instanceof Array) {
156155
if (options.query) {
@@ -613,6 +612,21 @@ function transformUpdateOperator(operator, flatten) {
613612
}
614613
}
615614

615+
const specialKeysForUntransform = [
616+
'_id',
617+
'_hashed_password',
618+
'_acl',
619+
'_email_verify_token',
620+
'_perishable_token',
621+
'_tombstone',
622+
'_session_token',
623+
'updatedAt',
624+
'_updated_at',
625+
'createdAt',
626+
'_created_at',
627+
'expiresAt',
628+
'_expiresAt',
629+
];
616630

617631
// Converts from a mongo-format object to a REST-format object.
618632
// Does not strip out anything based on a lack of authentication.
@@ -630,10 +644,9 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
630644
if (mongoObject === null) {
631645
return null;
632646
}
633-
634647
if (mongoObject instanceof Array) {
635-
return mongoObject.map((o) => {
636-
return untransformObject(schema, className, o, true);
648+
return mongoObject.map(arrayEntry => {
649+
return untransformObject(schema, className, arrayEntry, true);
637650
});
638651
}
639652

@@ -647,6 +660,10 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
647660

648661
var restObject = untransformACL(mongoObject);
649662
for (var key in mongoObject) {
663+
if (isNestedObject && _.includes(specialKeysForUntransform, key)) {
664+
restObject[key] = untransformObject(schema, className, mongoObject[key], true);
665+
continue;
666+
}
650667
switch(key) {
651668
case '_id':
652669
restObject['objectId'] = '' + mongoObject[key];
@@ -728,8 +745,7 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
728745
break;
729746
}
730747
}
731-
restObject[key] = untransformObject(schema, className,
732-
mongoObject[key], true);
748+
restObject[key] = untransformObject(schema, className, mongoObject[key], true);
733749
}
734750
}
735751

0 commit comments

Comments
 (0)