Skip to content

Commit 1412666

Browse files
authored
fix: Nested objects are encoded incorrectly for MongoDB (parse-community#8209)
BREAKING CHANGE: Nested objects are now properly stored in the database using JSON serialization; previously, due to a bug only top-level objects were serialized, but nested objects were saved as raw JSON; for example, a nested `Date` object was saved as a JSON object like `{ "__type": "Date", "iso": "2020-01-01T00:00:00.000Z" }` instead of its serialized representation `2020-01-01T00:00:00.000Z` (parse-community#8209)
1 parent 6323368 commit 1412666

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

spec/MongoStorageAdapter.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,44 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
212212
});
213213
});
214214

215+
it('handles nested dates', async () => {
216+
await new Parse.Object('MyClass', {
217+
foo: {
218+
test: {
219+
date: new Date(),
220+
},
221+
},
222+
bar: {
223+
date: new Date(),
224+
},
225+
date: new Date(),
226+
}).save();
227+
const adapter = Config.get(Parse.applicationId).database.adapter;
228+
const [object] = await adapter._rawFind('MyClass', {});
229+
expect(object.date instanceof Date).toBeTrue();
230+
expect(object.bar.date instanceof Date).toBeTrue();
231+
expect(object.foo.test.date instanceof Date).toBeTrue();
232+
});
233+
234+
it('handles nested dates in array ', async () => {
235+
await new Parse.Object('MyClass', {
236+
foo: {
237+
test: {
238+
date: [new Date()],
239+
},
240+
},
241+
bar: {
242+
date: [new Date()],
243+
},
244+
date: [new Date()],
245+
}).save();
246+
const adapter = Config.get(Parse.applicationId).database.adapter;
247+
const [object] = await adapter._rawFind('MyClass', {});
248+
expect(object.date[0] instanceof Date).toBeTrue();
249+
expect(object.bar.date[0] instanceof Date).toBeTrue();
250+
expect(object.foo.test.date[0] instanceof Date).toBeTrue();
251+
});
252+
215253
it('handles updating a single object with array, object date', done => {
216254
const adapter = new MongoStorageAdapter({ uri: databaseURI });
217255

src/Adapters/Storage/Mongo/MongoTransform.js

+3
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,9 @@ function mapValues(object, iterator) {
10141014
const result = {};
10151015
Object.keys(object).forEach(key => {
10161016
result[key] = iterator(object[key]);
1017+
if (result[key] && JSON.stringify(result[key]).includes(`"__type"`)) {
1018+
result[key] = mapValues(object[key], iterator);
1019+
}
10171020
});
10181021
return result;
10191022
}

0 commit comments

Comments
 (0)