Skip to content

fix: Nested objects are encoded incorrectly for MongoDB #8209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Dec 20, 2022
38 changes: 38 additions & 0 deletions spec/MongoStorageAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,44 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
});
});

it('handles nested dates', async () => {
await new Parse.Object('MyClass', {
foo: {
test: {
date: new Date(),
},
},
bar: {
date: new Date(),
},
date: new Date(),
}).save();
const adapter = Config.get(Parse.applicationId).database.adapter;
const [object] = await adapter._rawFind('MyClass', {});
expect(object.date instanceof Date).toBeTrue();
expect(object.bar.date instanceof Date).toBeTrue();
expect(object.foo.test.date instanceof Date).toBeTrue();
});

it('handles nested dates in array ', async () => {
await new Parse.Object('MyClass', {
foo: {
test: {
date: [new Date()],
},
},
bar: {
date: [new Date()],
},
date: [new Date()],
}).save();
const adapter = Config.get(Parse.applicationId).database.adapter;
const [object] = await adapter._rawFind('MyClass', {});
expect(object.date[0] instanceof Date).toBeTrue();
expect(object.bar.date[0] instanceof Date).toBeTrue();
expect(object.foo.test.date[0] instanceof Date).toBeTrue();
});

it('handles updating a single object with array, object date', done => {
const adapter = new MongoStorageAdapter({ uri: databaseURI });

Expand Down
3 changes: 3 additions & 0 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,9 @@ 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;
}
Expand Down