From 8c00b4432e4a36eb31865bbcb18798c00e0553bd Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 19 Jul 2016 02:14:32 -0400 Subject: [PATCH] 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 --- spec/CloudCode.spec.js | 19 +++++++++++++++++- spec/ParseAPI.spec.js | 17 ++++++++++++++++ src/Routers/FunctionsRouter.js | 36 ++++++++++++++++------------------ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 31774624563..3e5fdaa0296 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -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(); diff --git a/spec/ParseAPI.spec.js b/spec/ParseAPI.spec.js index 814cbb0aee7..a4ea9d902f2 100644 --- a/spec/ParseAPI.spec.js +++ b/spec/ParseAPI.spec.js @@ -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', diff --git a/src/Routers/FunctionsRouter.js b/src/Routers/FunctionsRouter.js index c8e82353b72..604a4a75ef1 100644 --- a/src/Routers/FunctionsRouter.js +++ b/src/Routers/FunctionsRouter.js @@ -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,