Skip to content
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

feat(ParseObject): enable subclasses to set initial values for props #924

Open
wants to merge 7 commits into
base: alpha
Choose a base branch
from
51 changes: 51 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1774,4 +1774,55 @@ describe('Parse Object', () => {
const fetched = await query.get(user.id);
assert.equal(fetched.isDataAvailable(), true);
});

it('retrieve subclass objects', async (done) => {
try {
class MySubclass extends Parse.Object {
constructor(attr) {
super('MySubclass', attr);
this.defaultProp = this.defaultProp || 'default';
}
get defaultProp() { return this.get('defaultProp'); }
set defaultProp(val) { this.set('defaultProp', val); }
}
Parse.Object.registerSubclass('MySubclass', MySubclass);

await new MySubclass({defaultProp: 'foo'}).save();
const result = await new Parse.Query(MySubclass).first();
expect(result.defaultProp).toBe('foo');

done();
} catch(e) {
done.fail(e);
}
});

it('conver from JSON', async (done) => {
try {

// Object without constructor
const o = Parse.Object.fromJSON({
className: 'FooObject',
name: 'foo'
}, true);

await o.save();
let result = await new Parse.Query('FooObject').first();
expect(result.get('name')).toBe('foo');

// Object with constructor `TestObject`
const to = Parse.Object.fromJSON({
className: 'TestObject',
name: 'foo'
}, true);

await to.save();
result = await new Parse.Query('TestObject').first();
expect(result.get('name')).toBe('foo');

done();
} catch(e) {
done.fail(e);
}
});
});
2 changes: 1 addition & 1 deletion integration/test/helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ParseServer = require('parse-server').ParseServer;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
beforeAll((done) => {
const { app } = require('../server');
const httpServer = require('http').createServer(app);
Expand Down
2 changes: 1 addition & 1 deletion src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ class ParseObject {
throw new Error('Cannot create an object without a className');
}
const constructor = classMap[json.className];
const o = constructor ? new constructor() : new ParseObject(json.className);
const o = constructor ? new constructor(json) : new ParseObject(json.className, Object.assign(json, { className: undefined }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const o = constructor ? new constructor(json) : new ParseObject(json.className) should be fine for your test to work.

There are other issues this brings up. There are other Subclasses of ParseObject like _User and _Session that will try to set readonly fields. You could try to clone the json and delete the readonly fields before passing to the constructor but I don't know what other issue this would cause.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RaschidJFR Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry I didn’t reply earlier. I’ve already done that with good results. Haven’t pushed though.

I’ve got almost all tests passing now, just a couple left. What I’m trying to figure out now is a way to prevent pending ops from overwriting the properties set in the constructor.

Tomorrow I’ll push my progress so anyone can have a look.

const otherAttributes = {};
for (const attr in json) {
if (attr !== 'className' && attr !== '__type') {
Expand Down