Skip to content

Commit

Permalink
Parses correctly Parse.Files and Dates when sent to Cloud Code Functi…
Browse files Browse the repository at this point in the history
…ons (#2297)

* fix for #2294

* fail tests

* Makes sure dates are compatible with Parse.com CloudCode #2214

* Adds regression tests for #2204
  • Loading branch information
flovilmart authored and drew-gross committed Jul 19, 2016
1 parent 4f89ec3 commit 8719afd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
19 changes: 18 additions & 1 deletion spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ describe('Cloud Code', () => {
expect(req.params.complexStructure.deepDate.date[0].getTime()).toBe(1463907600000);
expect(req.params.complexStructure.deepDate2[0].date instanceof Date).toBe(true);
expect(req.params.complexStructure.deepDate2[0].date.getTime()).toBe(1463907600000);
// Regression for #2294
expect(req.params.file instanceof Parse.File).toBe(true);
expect(req.params.file.url()).toEqual('https://some.url');
// Regression for #2204
expect(req.params.array).toEqual(['a', 'b', 'c']);
expect(Array.isArray(req.params.array)).toBe(true);
expect(req.params.arrayOfArray).toEqual([['a', 'b', 'c'], ['d', 'e','f']]);
expect(Array.isArray(req.params.arrayOfArray)).toBe(true);
expect(Array.isArray(req.params.arrayOfArray[0])).toBe(true);
expect(Array.isArray(req.params.arrayOfArray[1])).toBe(true);
return res.success({});
});

Expand Down Expand Up @@ -361,7 +371,14 @@ describe('Cloud Code', () => {
}
}
]
}
},
'file': Parse.File.fromJSON({
__type: 'File',
name: 'name',
url: 'https://some.url'
}),
'array': ['a', 'b', 'c'],
'arrayOfArray': [['a', 'b', 'c'], ['d', 'e', 'f']]
};
Parse.Cloud.run('params', params).then((result) => {
done();
Expand Down
17 changes: 17 additions & 0 deletions spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,23 @@ describe('miscellaneous', function() {
});
});

it('can handle date params in cloud functions (#2214)', done => {
let date = new Date();
Parse.Cloud.define('dateFunc', (request, response) => {
expect(request.params.date.__type).toEqual('Date');
expect(request.params.date.iso).toEqual(date.toISOString());
response.success('yay');
});

Parse.Cloud.run('dateFunc', {date: date})
.then(() => {
done()
}, e => {
fail('cloud code call failed');
done();
});
});

it('fails on invalid client key', done => {
var headers = {
'Content-Type': 'application/octet-stream',
Expand Down
36 changes: 17 additions & 19 deletions src/Routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@ var express = require('express'),
import PromiseRouter from '../PromiseRouter';
import _ from 'lodash';

function parseDate(params) {
return _.mapValues(params, (obj) => {
if (Array.isArray(obj)) {
function parseObject(obj) {
if (Array.isArray(obj)) {
return obj.map((item) => {
if (item && item.__type == 'Date') {
return new Date(item.iso);
} else if (item && typeof item === 'object') {
return parseDate(item);
} else {
return item;
}
return parseObject(item);
});
} else if (obj && obj.__type == 'Date') {
return new Date(obj.iso);
} else if (obj && typeof obj === 'object') {
return parseDate(obj);
} else {
return obj;
}
});
} else if (obj && obj.__type == 'Date') {
return Object.assign(new Date(obj.iso), obj);
} else if (obj && obj.__type == 'File') {
return Parse.File.fromJSON(obj);
} else if (obj && typeof obj === 'object') {
return parseParams(obj);
} else {
return obj;
}
}

function parseParams(params) {
return _.mapValues(params, parseObject);
}

export class FunctionsRouter extends PromiseRouter {
Expand Down Expand Up @@ -60,7 +58,7 @@ export class FunctionsRouter extends PromiseRouter {
var theValidator = triggers.getValidator(req.params.functionName, applicationId);
if (theFunction) {
let params = Object.assign({}, req.body, req.query);
params = parseDate(params);
params = parseParams(params);
var request = {
params: params,
master: req.auth && req.auth.isMaster,
Expand Down

0 comments on commit 8719afd

Please sign in to comment.