Skip to content

feat: generate Parse.Object.objectId automatically when allowCustomObjectId is enabled and no objectId is passed #1540

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 25 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
78729bb
feat: Update conditions for `MISSING_OBJECT_ID` error when allowCusto…
musthafa1996 Sep 4, 2022
a88e8ae
Update `MISSING_OBJECT_ID` error message
musthafa1996 Sep 4, 2022
ec80a44
Update tests
musthafa1996 Sep 4, 2022
2298855
Update integration/test/ParseObjectTest.js
musthafa1996 Sep 5, 2022
b3b3365
Update integration/test/ParseObjectTest.js
musthafa1996 Sep 5, 2022
91e425f
Fix tests
musthafa1996 Sep 6, 2022
2455fcb
Fix tests for custom object ID
musthafa1996 Sep 6, 2022
9f53d1a
Merge branch 'alpha' into allow-custom-id
mtrezza Sep 8, 2022
872aadb
Remove unnecessary id existence check from ParseObject in case allowC…
musthafa1996 Sep 9, 2022
43e96b3
Fix tests for allowCustomObjectId
musthafa1996 Sep 9, 2022
e8971a4
Merge branch 'alpha' into allow-custom-id
musthafa1996 Sep 10, 2022
55ecd1b
Add custom objectId validation back to ParseObject
musthafa1996 Sep 11, 2022
79fcf83
Fix ParseObject-test.js
musthafa1996 Sep 11, 2022
0399ee6
Fix package-lock.json
musthafa1996 Sep 12, 2022
b443f84
Cleanup ParseObjectTest.js
musthafa1996 Sep 12, 2022
ee5498e
Merge branch 'alpha' into allow-custom-id
mtrezza Sep 14, 2022
51f6678
Merge branch 'allow-custom-id' of github.com:musthafa1996/Parse-SDK-J…
musthafa1996 Sep 16, 2022
41e61b5
Merge branch 'alpha' into allow-custom-id
mtrezza Sep 16, 2022
2d6ffb7
Fix ParseObject-test.js
musthafa1996 Sep 19, 2022
7155872
Merge branch 'allow-custom-id' of github.com:musthafa1996/Parse-SDK-J…
musthafa1996 Sep 19, 2022
6bbb054
Merge branch 'alpha' into allow-custom-id
mtrezza Sep 19, 2022
7e3b18f
Split ParseOnject allowCustomObjectId integration tests
musthafa1996 Sep 21, 2022
04748d6
Merge branch 'allow-custom-id' of github.com:musthafa1996/Parse-SDK-J…
musthafa1996 Sep 21, 2022
7362468
Merge branch 'alpha' into allow-custom-id
mtrezza Sep 21, 2022
c5cc568
Merge branch 'alpha' into allow-custom-id
musthafa1996 Sep 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2063,18 +2063,32 @@ describe('Parse Object', () => {
it('allowCustomObjectId', async () => {
await reconfigureServer({ allowCustomObjectId: true });
Parse.allowCustomObjectId = true;
const customId = `${Date.now()}`;
const object = new Parse.Object('TestObject');

// Try to save without passing objectId
const object1 = new Parse.Object('TestObject');
try {
await object.save();
await object1.save();
} catch (error) {
fail();
}

// Try to save empty objectId
const object2 = new Parse.Object('TestObject');
object2.id = '';
try {
await object2.save();
fail();
} catch (error) {
expect(error.message).toBe('objectId must not be empty, null or undefined');
expect(error.message).toBe('objectId must not be empty');
}
object.id = customId;
object.set('foo', 'bar');
await object.save();
expect(object.id).toBe(customId);

// Try to save custom objectId
const customId = `${Date.now()}`;
const object3 = new Parse.Object('TestObject');
object3.id = customId;
object3.set('foo', 'bar');
await object3.save();
expect(object3.id).toBe(customId);

const query = new Parse.Query('TestObject');
const result = await query.get(customId);
Expand All @@ -2092,17 +2106,23 @@ describe('Parse Object', () => {
it('allowCustomObjectId saveAll', async () => {
await reconfigureServer({ allowCustomObjectId: true });
Parse.allowCustomObjectId = true;
const customId1 = `${Date.now()}`;
const customId2 = `${Date.now()}`;

// Try to save empty objectId
const obj1 = new TestObject({ foo: 'bar' });
obj1.id = '';
const obj2 = new TestObject({ foo: 'baz' });
obj2.id = '';
try {
await Parse.Object.saveAll([obj1, obj2]);
fail();
} catch (error) {
expect(error.message).toBe('objectId must not be empty, null or undefined');
expect(error.message).toBe('objectId must not be empty');
}

// Try to save custom objectId
const customId1 = `${Date.now()}`;
obj1.id = customId1;
const customId2 = `${Date.now()}`;
obj2.id = customId2;
await Parse.Object.saveAll([obj1, obj2]);
expect(obj1.id).toBe(customId1);
Expand Down
14 changes: 4 additions & 10 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2399,11 +2399,8 @@ const DefaultController = {
if (el instanceof ParseFile) {
filesSaved.push(el.save(options));
} else if (el instanceof ParseObject) {
if (allowCustomObjectId && !el.id) {
throw new ParseError(
ParseError.MISSING_OBJECT_ID,
'objectId must not be empty, null or undefined'
);
if (allowCustomObjectId && Object.prototype.hasOwnProperty.call(el, 'id') && !el.id) {
throw new ParseError(ParseError.MISSING_OBJECT_ID, 'objectId must not be empty');
}
pending.push(el);
}
Expand Down Expand Up @@ -2498,11 +2495,8 @@ const DefaultController = {
});
});
} else if (target instanceof ParseObject) {
if (allowCustomObjectId && !target.id) {
throw new ParseError(
ParseError.MISSING_OBJECT_ID,
'objectId must not be empty, null or undefined'
);
if (allowCustomObjectId && Object.prototype.hasOwnProperty.call(target, 'id') && !target.id) {
throw new ParseError(ParseError.MISSING_OBJECT_ID, 'objectId must not be empty');
}
// generate _localId in case if cascadeSave=false
target._getId();
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3810,23 +3810,24 @@ describe('ParseObject pin', () => {
it('can allowCustomObjectId', async done => {
CoreManager.set('ALLOW_CUSTOM_OBJECT_ID', true);
const o = new ParseObject('Person');
o.id = '';
let params = o._getSaveParams();
expect(params).toEqual({
method: 'POST',
body: { objectId: undefined },
body: { objectId: '' },
path: 'classes/Person',
});
try {
await o.save();
done.fail();
} catch (error) {
expect(error.message).toBe('objectId must not be empty, null or undefined');
expect(error.message).toBe('objectId must not be empty');
}
try {
await ParseObject.saveAll([o]);
done.fail();
} catch (error) {
expect(error.message).toBe('objectId must not be empty, null or undefined');
expect(error.message).toBe('objectId must not be empty');
}
o._finishFetch({
objectId: 'CUSTOM_ID',
Expand Down