From 11301d95900ad42c4e75f213f931eaa4ace4db48 Mon Sep 17 00:00:00 2001 From: Dan Huang Date: Fri, 10 Jun 2016 13:44:41 -0700 Subject: [PATCH] Fix an issue in MongoTransform where transforming object type fields with null values fails (#2029) --- spec/MongoTransform.spec.js | 9 +++++++++ spec/helper.js | 2 +- src/Adapters/Storage/Mongo/MongoTransform.js | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/spec/MongoTransform.spec.js b/spec/MongoTransform.spec.js index cb5baf77..56b377f7 100644 --- a/spec/MongoTransform.spec.js +++ b/spec/MongoTransform.spec.js @@ -16,6 +16,15 @@ describe('parseObjectToMongoObjectForCreate', () => { done(); }); + it('an object with null values', (done) => { + var input = {objectWithNullValues: {isNull: null, notNull: 3}}; + var output = transform.parseObjectToMongoObjectForCreate(null, input, { + fields: {objectWithNullValues: {type: 'object'}} + }); + jequal(input, output); + done(); + }); + it('built-in timestamps', (done) => { var input = { createdAt: "2015-10-06T21:24:50.332Z", diff --git a/spec/helper.js b/spec/helper.js index 43ad0dee..df2df591 100644 --- a/spec/helper.js +++ b/spec/helper.js @@ -209,7 +209,7 @@ function arrayContains(arr, item) { // Normalizes a JSON object. function normalize(obj) { - if (typeof obj !== 'object') { + if (obj === null || typeof obj !== 'object') { return JSON.stringify(obj); } if (obj instanceof Array) { diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index 898c62e0..b1d5170b 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -83,7 +83,7 @@ const transformKeyValueForUpdate = (className, restKey, restValue, parseFormatSc } const transformInteriorValue = restValue => { - if (typeof restValue === 'object' && Object.keys(restValue).some(key => key.includes('$') || key.includes('.'))) { + if (restValue !== null && typeof restValue === 'object' && Object.keys(restValue).some(key => key.includes('$') || key.includes('.'))) { throw new Parse.Error(Parse.Error.INVALID_NESTED_KEY, "Nested keys should not contain the '$' or '.' characters"); } // Handle atomic values