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

Fixes #1417 #1420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions spec/transform.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// These tests are unit tests designed to only test transform.js.
"use strict";

var transform = require('../src/transform');
let transform = require('../src/transform');
let dd = require('deep-diff');

var dummySchema = {
data: {},
Expand Down Expand Up @@ -150,14 +152,30 @@ describe('untransformObject', () => {
done();
});

it('nested array', (done) => {
var input = {arr: [{_testKey: 'testValue' }]};
var output = transform.untransformObject(dummySchema, null, input);
expect(Array.isArray(output.arr)).toEqual(true);
expect(output.arr).toEqual([{ _testKey: 'testValue'}]);
done();
});
it('nested array', (done) => {
var input = {arr: [{_testKey: 'testValue' }]};
var output = transform.untransformObject(dummySchema, null, input);
expect(Array.isArray(output.arr)).toEqual(true);
expect(output.arr).toEqual([{ _testKey: 'testValue'}]);
done();
});

it('untransforms objects containing nested special keys', done => {
let input = {array: [{
_id: "Test ID",
_hashed_password: "I Don't know why you would name a key this, but if you do it should work",
_tombstone: {
_updated_at: "I'm sure people will nest keys like this",
_acl: 7,
_id: { someString: "str", someNumber: 7},
regularKey: { moreContents: [1, 2, 3] },
},
regularKey: "some data",
}]}
let output = transform.untransformObject(dummySchema, null, input);
expect(dd(output, input)).toEqual(undefined);
done();
});
});

describe('transformKey', () => {
Expand Down
30 changes: 23 additions & 7 deletions src/transform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import log from './logger';
import _ from 'lodash';
var mongodb = require('mongodb');
var Parse = require('parse/node').Parse;

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



// Handle arrays
if (restValue instanceof Array) {
if (options.query) {
Expand Down Expand Up @@ -613,6 +612,21 @@ function transformUpdateOperator(operator, flatten) {
}
}

const specialKeysForUntransform = [
'_id',
'_hashed_password',
'_acl',
'_email_verify_token',
'_perishable_token',
'_tombstone',
'_session_token',
'updatedAt',
'_updated_at',
'createdAt',
'_created_at',
'expiresAt',
'_expiresAt',
];

// Converts from a mongo-format object to a REST-format object.
// Does not strip out anything based on a lack of authentication.
Expand All @@ -630,10 +644,9 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
if (mongoObject === null) {
return null;
}

if (mongoObject instanceof Array) {
return mongoObject.map((o) => {
return untransformObject(schema, className, o, true);
return mongoObject.map(arrayEntry => {
return untransformObject(schema, className, arrayEntry, true);
});
}

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we do something like

if (isNestedObject) {
   let transformableKeys = ["_id", ...];
   Object.keys(mongoObject).forEach((key) => {
     if (transformableKeys.indexOf(key) > -1) {
       restObject[key] = untransformObject(schema, className, mongoObject[key], true);
     } else {
       restObject[key] = mongoObject[key];
     }
  });
  return restObject;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I will refactor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move let transformableKeys some where else, and if we get crazy we could have a structure like:

transformKey: {
   key: {
       nested: (key, mongoObject, restObject) => {
           // to the mongoObjectTransform and assign on restObject
       },
       default: (key, mongoObject, restObject) => {
           // to the mongoObjectTransform and assign on restObject
       },
   }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with something simpler. Moving this logic all to the mongo adapter will be a better time to fix it up.

var restObject = untransformACL(mongoObject);
for (var key in mongoObject) {
if (isNestedObject && _.includes(specialKeysForUntransform, key)) {
restObject[key] = untransformObject(schema, className, mongoObject[key], true);
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're not setting restObject[key] = mongoObject[key]

}
switch(key) {
case '_id':
restObject['objectId'] = '' + mongoObject[key];
Expand Down Expand Up @@ -728,8 +745,7 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
break;
}
}
restObject[key] = untransformObject(schema, className,
mongoObject[key], true);
restObject[key] = untransformObject(schema, className, mongoObject[key], true);
}
}

Expand Down