From 22d2446dfea2bc339affc20535d181097e152acf Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 5 Mar 2023 11:22:19 +1100 Subject: [PATCH] fix: Nested date is incorrectly decoded as empty object `{}` when fetching a Parse Object (#8446) --- spec/MongoStorageAdapter.spec.js | 4 ++++ src/Adapters/Storage/Mongo/MongoTransform.js | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spec/MongoStorageAdapter.spec.js b/spec/MongoStorageAdapter.spec.js index 58731d2432..06fdab4fca 100644 --- a/spec/MongoStorageAdapter.spec.js +++ b/spec/MongoStorageAdapter.spec.js @@ -248,6 +248,10 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { expect(object.date[0] instanceof Date).toBeTrue(); expect(object.bar.date[0] instanceof Date).toBeTrue(); expect(object.foo.test.date[0] instanceof Date).toBeTrue(); + const obj = await new Parse.Query('MyClass').first({useMasterKey: true}); + expect(obj.get('date')[0] instanceof Date).toBeTrue(); + expect(obj.get('bar').date[0] instanceof Date).toBeTrue(); + expect(obj.get('foo').test.date[0] instanceof Date).toBeTrue(); }); it('handles updating a single object with array, object date', done => { diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index aabf744978..6f6811cec3 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -188,6 +188,16 @@ const transformInteriorValue = restValue => { // Handle atomic values var value = transformInteriorAtom(restValue); if (value !== CannotTransform) { + if (value && typeof value === 'object') { + if (value instanceof Date) { + return value; + } + if (value instanceof Array) { + value = value.map(transformInteriorValue); + } else { + value = mapValues(value, transformInteriorValue); + } + } return value; } @@ -1014,9 +1024,6 @@ function mapValues(object, iterator) { const result = {}; Object.keys(object).forEach(key => { result[key] = iterator(object[key]); - if (result[key] && JSON.stringify(result[key]).includes(`"__type"`)) { - result[key] = mapValues(object[key], iterator); - } }); return result; }