diff --git a/spec/ParseRelation.spec.js b/spec/ParseRelation.spec.js index ad5ba361..c7807819 100644 --- a/spec/ParseRelation.spec.js +++ b/spec/ParseRelation.spec.js @@ -801,4 +801,37 @@ describe('Parse.Relation testing', () => { done(); }); }); + + it('ensures beforeFind on relation doesnt side effect', (done) => { + const parent = new Parse.Object('Parent'); + const child = new Parse.Object('Child'); + child.save().then(() => { + parent.relation('children').add(child); + return parent.save(); + }).then(() => { + // We need to use a new reference otherwise the JS SDK remembers the className for a relation + // After saves or finds + const otherParent = new Parse.Object('Parent'); + otherParent.id = parent.id; + return otherParent.relation('children').query().find(); + }).then((children) => { + // Without an after find all is good, all results have been redirected with proper className + children.forEach((child) => expect(child.className).toBe('Child')); + // Setup the afterFind + Parse.Cloud.afterFind('Child', (req) => { + return Promise.resolve(req.objects.map((child) => { + child.set('afterFound', true); + return child; + })); + }); + const otherParent = new Parse.Object('Parent'); + otherParent.id = parent.id; + return otherParent.relation('children').query().find(); + }).then((children) => { + children.forEach((child) => { + expect(child.className).toBe('Child'); + expect(child.get('afterFound')).toBe(true); + }); + }).then(done).catch(done.fail); + }); }); diff --git a/src/RestQuery.js b/src/RestQuery.js index 25e5d513..8bf8d529 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -590,7 +590,18 @@ RestQuery.prototype.runAfterFindTrigger = function() { } // Run afterFind trigger and set the new results return triggers.maybeRunAfterFindTrigger(triggers.Types.afterFind, this.auth, this.className,this.response.results, this.config).then((results) => { - this.response.results = results; + // Ensure we properly set the className back + if (this.redirectClassName) { + this.response.results = results.map((object) => { + if (object instanceof Parse.Object) { + object = object.toJSON(); + } + object.className = this.redirectClassName; + return object; + }); + } else { + this.response.results = results; + } }); };