Skip to content

Commit 575e128

Browse files
committed
Make sure createdAt/updatedAt fields are encoded as strings on nested objects
1 parent 19a8420 commit 575e128

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/__tests__/ParseObject-test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,63 @@ describe('ParseObject', () => {
570570
});
571571
});
572572

573+
it('properly encodes createdAt/updatedAt dates on nested objects', () => {
574+
var o = ParseObject.fromJSON({
575+
__type: 'Object',
576+
className: 'Item',
577+
objectId: 'recurParent',
578+
createdAt: '1970-01-01T00:00:00.000Z',
579+
updatedAt: '1970-01-01T00:00:00.000Z',
580+
aDate: {
581+
__type: 'Date',
582+
iso: '1970-01-01T00:00:00.000Z'
583+
},
584+
child: {
585+
__type: 'Pointer',
586+
className: 'Item',
587+
objectId: 'recurChild'
588+
}
589+
});
590+
expect(o.createdAt.getTime()).toBe(new Date(0).getTime());
591+
expect(o.updatedAt.getTime()).toBe(new Date(0).getTime());
592+
expect(o.get('aDate').getTime()).toBe(new Date(0).getTime());
593+
594+
var child = ParseObject.fromJSON({
595+
__type: 'Object',
596+
className: 'Item',
597+
objectId: 'recurChild',
598+
createdAt: '1970-01-01T00:00:00.000Z',
599+
updatedAt: '1970-01-01T00:00:00.000Z',
600+
parent: {
601+
__type: 'Pointer',
602+
className: 'Item',
603+
objectId: 'recurParent'
604+
}
605+
});
606+
607+
expect(o.toJSON()).toEqual({
608+
objectId: 'recurParent',
609+
createdAt: '1970-01-01T00:00:00.000Z',
610+
updatedAt: '1970-01-01T00:00:00.000Z',
611+
aDate: {
612+
__type: 'Date',
613+
iso: '1970-01-01T00:00:00.000Z'
614+
},
615+
child: {
616+
__type: 'Object',
617+
className: 'Item',
618+
objectId: 'recurChild',
619+
createdAt: '1970-01-01T00:00:00.000Z',
620+
updatedAt: '1970-01-01T00:00:00.000Z',
621+
parent: {
622+
__type: 'Pointer',
623+
className: 'Item',
624+
objectId: 'recurParent'
625+
}
626+
}
627+
});
628+
});
629+
573630
it('updates the existed flag when saved', () => {
574631
var o = new ParseObject('Item');
575632
expect(o.existed()).toBe(false);

src/encode.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ function encode(value: mixed, disallowObjects: boolean, forcePointers: boolean,
3434
}
3535
seen = seen.concat(seenEntry);
3636
var json = encode(value.attributes, disallowObjects, forcePointers, seen);
37+
if (json.createdAt) {
38+
json.createdAt = json.createdAt.iso;
39+
}
40+
if (json.updatedAt) {
41+
json.updatedAt = json.updatedAt.iso;
42+
}
3743
json.className = value.className;
3844
json.__type = 'Object';
3945
if (value.id) {

0 commit comments

Comments
 (0)