Skip to content

Commit fa05044

Browse files
authored
fix(ParseRelation): Schema mismatch on empty array (#1222)
* fix(ParseRelation): Schema mismatch on empty array Closes: #1217 * Improve coverage
1 parent 59d0a68 commit fa05044

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

integration/test/ParseRelationTest.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const assert = require('assert');
44
const clear = require('./clear');
55
const Parse = require('../../node');
66

7+
const TestObject = Parse.Object.extend('TestObject');
8+
79
describe('Parse Relation', () => {
810
beforeEach((done) => {
911
Parse.initialize('integration', null, 'notsosecret');
@@ -211,4 +213,38 @@ describe('Parse Relation', () => {
211213
done();
212214
});
213215
});
216+
217+
it('can add empty array to relation', async () => {
218+
const object1 = new TestObject();
219+
await object1.save();
220+
221+
const object2 = new TestObject();
222+
object2.relation('related').add(object1);
223+
await object2.save();
224+
225+
object2.relation('related').add([]);
226+
await object2.save();
227+
228+
const relation = object2.relation('related');
229+
const query = relation.query();
230+
const results = await query.find();
231+
expect(results.length).toBe(1);
232+
});
233+
234+
it('can remove empty array from relation', async () => {
235+
const object1 = new TestObject();
236+
await object1.save();
237+
238+
const object2 = new TestObject();
239+
object2.relation('related').add(object1);
240+
await object2.save();
241+
242+
object2.relation('related').remove([]);
243+
await object2.save();
244+
245+
const relation = object2.relation('related');
246+
const query = relation.query();
247+
const results = await query.find();
248+
expect(results.length).toBe(1);
249+
});
214250
});

src/ParseRelation.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class ParseRelation {
8585
if (!parent) {
8686
throw new Error('Cannot add to a Relation without a parent');
8787
}
88+
if (objects.length === 0) {
89+
return parent;
90+
}
8891
parent.set(this.key, change);
8992
this.targetClassName = change._targetClassName;
9093
return parent;
@@ -104,6 +107,9 @@ class ParseRelation {
104107
if (!this.parent) {
105108
throw new Error('Cannot remove from a Relation without a parent');
106109
}
110+
if (objects.length === 0) {
111+
return;
112+
}
107113
this.parent.set(this.key, change);
108114
this.targetClassName = change._targetClassName;
109115
}

src/__tests__/ParseRelation-test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@ describe('ParseRelation', () => {
118118
});
119119
});
120120

121+
it('can add empty array to a relation', () => {
122+
const parent = new ParseObject('Item');
123+
parent.id = 'I1234';
124+
const r = new ParseRelation(parent, 'shipments');
125+
const o = new ParseObject('Delivery');
126+
o.id = 'D1234';
127+
const p = r.add(o);
128+
expect(p).toBeTruthy();
129+
expect(r.toJSON()).toEqual({
130+
__type: 'Relation',
131+
className: 'Delivery'
132+
});
133+
expect(parent.op('shipments').toJSON()).toEqual({
134+
__op: 'AddRelation',
135+
objects: [
136+
{ __type: 'Pointer', objectId: 'D1234', className: 'Delivery' }
137+
]
138+
});
139+
// Adding empty array shouldn't change the relation
140+
r.add([]);
141+
expect(r.toJSON()).toEqual({
142+
__type: 'Relation',
143+
className: 'Delivery'
144+
});
145+
expect(parent.op('shipments').toJSON()).toEqual({
146+
__op: 'AddRelation',
147+
objects: [
148+
{ __type: 'Pointer', objectId: 'D1234', className: 'Delivery' }
149+
]
150+
});
151+
});
152+
121153
it('can remove objects from a relation', () => {
122154
const parent = new ParseObject('Item');
123155
parent.id = 'I2';
@@ -155,6 +187,37 @@ describe('ParseRelation', () => {
155187
});
156188
});
157189

190+
it('can remove empty array from a relation', () => {
191+
const parent = new ParseObject('Item');
192+
parent.id = 'I5678';
193+
const r = new ParseRelation(parent, 'shipments');
194+
const o = new ParseObject('Delivery');
195+
o.id = 'D5678';
196+
r.remove(o);
197+
expect(r.toJSON()).toEqual({
198+
__type: 'Relation',
199+
className: 'Delivery'
200+
});
201+
expect(parent.op('shipments').toJSON()).toEqual({
202+
__op: 'RemoveRelation',
203+
objects: [
204+
{ __type: 'Pointer', objectId: 'D5678', className: 'Delivery' }
205+
]
206+
});
207+
// Removing empty array shouldn't change the relation
208+
r.remove([]);
209+
expect(r.toJSON()).toEqual({
210+
__type: 'Relation',
211+
className: 'Delivery'
212+
});
213+
expect(parent.op('shipments').toJSON()).toEqual({
214+
__op: 'RemoveRelation',
215+
objects: [
216+
{ __type: 'Pointer', objectId: 'D5678', className: 'Delivery' }
217+
]
218+
});
219+
});
220+
158221
it('can generate a query for relation objects', () => {
159222
const parent = new ParseObject('Item');
160223
parent.id = 'I1';

0 commit comments

Comments
 (0)