Skip to content

Commit 8719afd

Browse files
flovilmartdrew-gross
authored andcommitted
Parses correctly Parse.Files and Dates when sent to Cloud Code Functions (#2297)
* fix for #2294 * fail tests * Makes sure dates are compatible with Parse.com CloudCode #2214 * Adds regression tests for #2204
1 parent 4f89ec3 commit 8719afd

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

Diff for: spec/CloudCode.spec.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ describe('Cloud Code', () => {
323323
expect(req.params.complexStructure.deepDate.date[0].getTime()).toBe(1463907600000);
324324
expect(req.params.complexStructure.deepDate2[0].date instanceof Date).toBe(true);
325325
expect(req.params.complexStructure.deepDate2[0].date.getTime()).toBe(1463907600000);
326+
// Regression for #2294
327+
expect(req.params.file instanceof Parse.File).toBe(true);
328+
expect(req.params.file.url()).toEqual('https://some.url');
329+
// Regression for #2204
330+
expect(req.params.array).toEqual(['a', 'b', 'c']);
331+
expect(Array.isArray(req.params.array)).toBe(true);
332+
expect(req.params.arrayOfArray).toEqual([['a', 'b', 'c'], ['d', 'e','f']]);
333+
expect(Array.isArray(req.params.arrayOfArray)).toBe(true);
334+
expect(Array.isArray(req.params.arrayOfArray[0])).toBe(true);
335+
expect(Array.isArray(req.params.arrayOfArray[1])).toBe(true);
326336
return res.success({});
327337
});
328338

@@ -361,7 +371,14 @@ describe('Cloud Code', () => {
361371
}
362372
}
363373
]
364-
}
374+
},
375+
'file': Parse.File.fromJSON({
376+
__type: 'File',
377+
name: 'name',
378+
url: 'https://some.url'
379+
}),
380+
'array': ['a', 'b', 'c'],
381+
'arrayOfArray': [['a', 'b', 'c'], ['d', 'e', 'f']]
365382
};
366383
Parse.Cloud.run('params', params).then((result) => {
367384
done();

Diff for: spec/ParseAPI.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,23 @@ describe('miscellaneous', function() {
10381038
});
10391039
});
10401040

1041+
it('can handle date params in cloud functions (#2214)', done => {
1042+
let date = new Date();
1043+
Parse.Cloud.define('dateFunc', (request, response) => {
1044+
expect(request.params.date.__type).toEqual('Date');
1045+
expect(request.params.date.iso).toEqual(date.toISOString());
1046+
response.success('yay');
1047+
});
1048+
1049+
Parse.Cloud.run('dateFunc', {date: date})
1050+
.then(() => {
1051+
done()
1052+
}, e => {
1053+
fail('cloud code call failed');
1054+
done();
1055+
});
1056+
});
1057+
10411058
it('fails on invalid client key', done => {
10421059
var headers = {
10431060
'Content-Type': 'application/octet-stream',

Diff for: src/Routers/FunctionsRouter.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,24 @@ var express = require('express'),
77
import PromiseRouter from '../PromiseRouter';
88
import _ from 'lodash';
99

10-
function parseDate(params) {
11-
return _.mapValues(params, (obj) => {
12-
if (Array.isArray(obj)) {
10+
function parseObject(obj) {
11+
if (Array.isArray(obj)) {
1312
return obj.map((item) => {
14-
if (item && item.__type == 'Date') {
15-
return new Date(item.iso);
16-
} else if (item && typeof item === 'object') {
17-
return parseDate(item);
18-
} else {
19-
return item;
20-
}
13+
return parseObject(item);
2114
});
22-
} else if (obj && obj.__type == 'Date') {
23-
return new Date(obj.iso);
24-
} else if (obj && typeof obj === 'object') {
25-
return parseDate(obj);
26-
} else {
27-
return obj;
28-
}
29-
});
15+
} else if (obj && obj.__type == 'Date') {
16+
return Object.assign(new Date(obj.iso), obj);
17+
} else if (obj && obj.__type == 'File') {
18+
return Parse.File.fromJSON(obj);
19+
} else if (obj && typeof obj === 'object') {
20+
return parseParams(obj);
21+
} else {
22+
return obj;
23+
}
24+
}
25+
26+
function parseParams(params) {
27+
return _.mapValues(params, parseObject);
3028
}
3129

3230
export class FunctionsRouter extends PromiseRouter {
@@ -60,7 +58,7 @@ export class FunctionsRouter extends PromiseRouter {
6058
var theValidator = triggers.getValidator(req.params.functionName, applicationId);
6159
if (theFunction) {
6260
let params = Object.assign({}, req.body, req.query);
63-
params = parseDate(params);
61+
params = parseParams(params);
6462
var request = {
6563
params: params,
6664
master: req.auth && req.auth.isMaster,

0 commit comments

Comments
 (0)