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
This commit is contained in:
@@ -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({});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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 {
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user