From d798aa3517b0a38f6daa7e6798fa3bea997f06b8 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Sat, 20 Oct 2018 19:14:26 -0500 Subject: [PATCH 1/2] Handle undefined in Cloud Code --- integration/cloud/main.js | 4 ++++ integration/test/ParseCloudTest.js | 7 +++++++ src/Cloud.js | 6 +----- src/__tests__/Cloud-test.js | 7 +++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/integration/cloud/main.js b/integration/cloud/main.js index d677209b5..e15437422 100644 --- a/integration/cloud/main.js +++ b/integration/cloud/main.js @@ -7,6 +7,10 @@ Parse.Cloud.define("bar", function(request) { } }); +Parse.Cloud.define('CloudFunctionUndefined', function() { + return undefined; +}); + Parse.Cloud.job('CloudJob1', function() { return { status: 'cloud job completed' diff --git a/integration/test/ParseCloudTest.js b/integration/test/ParseCloudTest.js index 062e12f1b..958810aa4 100644 --- a/integration/test/ParseCloudTest.js +++ b/integration/test/ParseCloudTest.js @@ -70,6 +70,13 @@ describe('Parse Cloud', () => { } }); + it('run function with undefined', (done) => { + Parse.Cloud.run('CloudFunctionUndefined', {}).then((result) => { + assert.equal(result, null); + done(); + }); + }); + it('run job', (done) => { const params = { startedBy: 'Monty Python' }; Parse.Cloud.startJob('CloudJob1', params).then((jobStatusId) => { diff --git a/src/Cloud.js b/src/Cloud.js index 09d90e5e5..767efbfec 100644 --- a/src/Cloud.js +++ b/src/Cloud.js @@ -12,7 +12,6 @@ import CoreManager from './CoreManager'; import decode from './decode'; import encode from './encode'; -import ParseError from './ParseError'; import ParseQuery from './ParseQuery'; /** @@ -126,10 +125,7 @@ const DefaultController = { if (decoded && decoded.hasOwnProperty('result')) { return Promise.resolve(decoded.result); } - throw new ParseError( - ParseError.INVALID_JSON, - 'The server returned an invalid response.' - ); + return Promise.resolve(null); }); }, diff --git a/src/__tests__/Cloud-test.js b/src/__tests__/Cloud-test.js index b8e083b4c..d39c1fa49 100644 --- a/src/__tests__/Cloud-test.js +++ b/src/__tests__/Cloud-test.js @@ -151,13 +151,12 @@ describe('CloudController', () => { it('run invalid response', (done) => { const request = jest.fn(); - request.mockReturnValue(Promise.resolve({ - success: false - })); + request.mockReturnValue(Promise.resolve(undefined)); + const ajax = jest.fn(); CoreManager.setRESTController({ request: request, ajax: ajax }); - Cloud.run('myfunction').then(null).catch(() => { + Cloud.run('myfunction').then(() => { done(); }); }); From 0eb0e7e835261d0ed0e67898f0fd43e8ef147a85 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Sat, 20 Oct 2018 22:59:32 -0500 Subject: [PATCH 2/2] properly handle undefined --- integration/test/ParseCloudTest.js | 2 +- src/Cloud.js | 11 ++++++++++- src/__tests__/Cloud-test.js | 13 +++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/integration/test/ParseCloudTest.js b/integration/test/ParseCloudTest.js index 958810aa4..42770a896 100644 --- a/integration/test/ParseCloudTest.js +++ b/integration/test/ParseCloudTest.js @@ -72,7 +72,7 @@ describe('Parse Cloud', () => { it('run function with undefined', (done) => { Parse.Cloud.run('CloudFunctionUndefined', {}).then((result) => { - assert.equal(result, null); + assert.strictEqual(result, undefined); done(); }); }); diff --git a/src/Cloud.js b/src/Cloud.js index 767efbfec..c147a1d53 100644 --- a/src/Cloud.js +++ b/src/Cloud.js @@ -12,6 +12,7 @@ import CoreManager from './CoreManager'; import decode from './decode'; import encode from './encode'; +import ParseError from './ParseError'; import ParseQuery from './ParseQuery'; /** @@ -121,11 +122,19 @@ const DefaultController = { ); return request.then((res) => { + if (typeof res === 'object' && + Object.keys(res).length > 0 && + !res.hasOwnProperty('result')) { + throw new ParseError( + ParseError.INVALID_JSON, + 'The server returned an invalid response.' + ); + } const decoded = decode(res); if (decoded && decoded.hasOwnProperty('result')) { return Promise.resolve(decoded.result); } - return Promise.resolve(null); + return Promise.resolve(undefined); }); }, diff --git a/src/__tests__/Cloud-test.js b/src/__tests__/Cloud-test.js index d39c1fa49..2a5b9bfda 100644 --- a/src/__tests__/Cloud-test.js +++ b/src/__tests__/Cloud-test.js @@ -150,6 +150,19 @@ describe('CloudController', () => { }); it('run invalid response', (done) => { + const request = jest.fn(); + request.mockReturnValue(Promise.resolve({ + success: false + })); + const ajax = jest.fn(); + CoreManager.setRESTController({ request: request, ajax: ajax }); + + Cloud.run('myfunction').then(null).catch(() => { + done(); + }); + }); + + it('run undefined response', (done) => { const request = jest.fn(); request.mockReturnValue(Promise.resolve(undefined));