Skip to content

Commit

Permalink
Merge 3475107 into e9d8ed4
Browse files Browse the repository at this point in the history
  • Loading branch information
mstniy authored May 24, 2021
2 parents e9d8ed4 + 3475107 commit a18687a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
22 changes: 22 additions & 0 deletions spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,28 @@ describe('miscellaneous', function () {
});
});

it_only_db('mongo')('pointer reassign on nested fields is working properly (#7391)', async () => {
const obj = new Parse.Object('GameScore'); // This object will include nested pointers
const ptr1 = new Parse.Object('GameScore');
await ptr1.save(); // Obtain a unique id
const ptr2 = new Parse.Object('GameScore');
await ptr2.save(); // Obtain a unique id
obj.set('data', { ptr: ptr1 });
await obj.save();

obj.set('data.ptr', ptr2);
await obj.save();

const obj2 = await new Parse.Query('GameScore').get(obj.id);
expect(obj2.get('data').ptr.id).toBe(ptr2.id);

const query = new Parse.Query('GameScore');
query.equalTo('data.ptr', ptr2);
const res = await query.find();
expect(res.length).toBe(1);
expect(res[0].get('data').ptr.id).toBe(ptr2.id);
});

it('test afterSave get full object on create and update', function (done) {
let triggerTime = 0;
// Register a mock beforeSave hook
Expand Down
17 changes: 13 additions & 4 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ const transformKeyValueForUpdate = (className, restKey, restValue, parseFormatSc

if (
(parseFormatSchema.fields[key] && parseFormatSchema.fields[key].type === 'Pointer') ||
(!parseFormatSchema.fields[key] && restValue && restValue.__type == 'Pointer')
(!key.includes('.') &&
!parseFormatSchema.fields[key] &&
restValue &&
restValue.__type == 'Pointer') // Do not use the _p_ prefix for pointers inside nested documents
) {
key = '_p_' + key;
}
Expand Down Expand Up @@ -305,7 +308,10 @@ function transformQueryKeyValue(className, key, value, schema, count = false) {
schema && schema.fields[key] && schema.fields[key].type === 'Pointer';

const field = schema && schema.fields[key];
if (expectedTypeIsPointer || (!schema && value && value.__type === 'Pointer')) {
if (
expectedTypeIsPointer ||
(!schema && !key.includes('.') && value && value.__type === 'Pointer')
) {
key = '_p_' + key;
}

Expand All @@ -326,8 +332,11 @@ function transformQueryKeyValue(className, key, value, schema, count = false) {
}

// Handle atomic values
if (transformTopLevelAtom(value) !== CannotTransform) {
return { key, value: transformTopLevelAtom(value) };
const transformRes = key.includes('.')
? transformInteriorAtom(value)
: transformTopLevelAtom(value);
if (transformRes !== CannotTransform) {
return { key, value: transformRes };
} else {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
Expand Down

0 comments on commit a18687a

Please sign in to comment.