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

fix: Parse.Object.get returns array instead of object if key name is number-like #2201

Merged
merged 15 commits into from
Jul 7, 2024
38 changes: 38 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,44 @@ describe('Parse Object', () => {
expect(obj.get('string')).toBeInstanceOf(String);
});

it('returns correct field values', async () => {
const values = [
{ field: 'string', value: 'string' },
{ field: 'number', value: 1 },
{ field: 'boolean', value: true },
{ field: 'array', value: [0, 1, 2] },
{ field: 'array', value: [1, 2, 3] },
{ field: 'array', value: [{ '0': 'a' }, 2, 3] },
{ field: 'object', value: { key: 'value' } },
{ field: 'object', value: { key1: 'value1', key2: 'value2' } },
{ field: 'object', value: { key1: 1, key2: 2 } },
{ field: 'object', value: { '1x1': 1 } },
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
{ field: 'object', value: { '1x1': 1, '2': 2 } },
{ field: 'object', value: { '0': 0 } },
{ field: 'object', value: { '1': 1 } },
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
{ field: 'object', value: { '0': { '0': 'a', '1': 'b' } } },
{ field: 'date', value: new Date() },
{
field: 'file',
value: Parse.File.fromJSON({
__type: 'File',
name: 'name',
url: 'http://localhost:1337/parse/files/integration/name',
}),
},
{ field: 'geoPoint', value: new Parse.GeoPoint(40, -30) },
{ field: 'bytes', value: { __type: 'Bytes', base64: 'ZnJveW8=' } },
];
for (const value of values) {
const object = new TestObject();
object.set(value.field, value.value);
await object.save();
const query = new Parse.Query(TestObject);
const objectAgain = await query.get(object.id);
expect(objectAgain.get(value.field)).toEqual(value.value);
}
});

describe('allowCustomObjectId', () => {
it('can save without setting an objectId', async () => {
await reconfigureServer({ allowCustomObjectId: true });
Expand Down
13 changes: 1 addition & 12 deletions src/ObjectStateMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,7 @@ export function commitServerChanges(
) {
const ParseObject = CoreManager.getParseObject();
for (const attr in changes) {
let val = changes[attr];
// Check for JSON array { '0': { something }, '1': { something } }
if (
val &&
typeof val === 'object' &&
!Array.isArray(val) &&
Object.keys(val).length > 0 &&
Object.keys(val).some(k => !isNaN(parseInt(k))) &&
!['sentPerUTCOffset', 'failedPerUTCOffset'].includes(attr)
) {
val = Object.values(val);
}
const val = changes[attr];
nestedSet(serverData, attr, val);
if (
val &&
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/ObjectStateMutations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,9 @@ describe('ObjectStateMutations', () => {
ObjectStateMutations.commitServerChanges(serverData, objectCache, {
items: { '0': { count: 20 }, '1': { count: 5 } },
});
expect(serverData).toEqual({ items: [{ count: 20 }, { count: 5 }] });
expect(objectCache).toEqual({ items: '[{"count":20},{"count":5}]' });
// Should not transform
expect(serverData).toEqual({ items: { '0': { count: 20 }, '1': { count: 5 } } });
expect(objectCache).toEqual({ items: '{"0":{"count":20},"1":{"count":5}}' });
});

mtrezza marked this conversation as resolved.
Show resolved Hide resolved
it('can commit json array with PushStatus offset fields', () => {
Expand Down
Loading