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

Parses correctly Parse.Files and Dates when sent to Cloud Code Functions #2297

Merged
merged 4 commits into from
Jul 19, 2016
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
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