Skip to content

fix: Objects wrongly getting converted to Array #2206

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2187,4 +2187,37 @@ describe('Parse Object', () => {
Parse.allowCustomObjectId = false;
});
});

fit('returns correct field values', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove fit, clean up the test and add any additional values you can think of?

Also can you add a unit test to ensure the internals still work this change to JSONArray.
https://github.com/parse-community/Parse-SDK-JS/blob/alpha/src/__tests__/ObjectStateMutations-test.js#L316

Copy link
Member

@mtrezza mtrezza Jul 5, 2024

Choose a reason for hiding this comment

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

Please keep fit until you have added and uncommented all the values from #2201 and the test passes.

const values = [
{ field: 'string', value: 'string' },
{ field: 'number', value: 1 },
{ field: 'boolean', value: true },
{ field: 'array', value: [1, 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, '2': 2, '3': 3 } },
{ field: 'object', value: { '1': 1 } },
{ 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);
}
});
});
10 changes: 8 additions & 2 deletions src/ObjectStateMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ function nestedSet(obj, key, value) {
}
}

function isJSONArray(val) {
// Check for JSON array { '0': { something }, '1': { something } }
const keys = Object.keys(val);
const indexes = keys.map((k, i) => `${i}`);
return keys.every(k => indexes.includes(k));
}

export function commitServerChanges(
serverData: AttributeMap,
objectCache: ObjectCache,
Expand All @@ -185,13 +192,12 @@ 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))) &&
isJSONArray(val) &&
!['sentPerUTCOffset', 'failedPerUTCOffset'].includes(attr)
) {
val = Object.values(val);
Expand Down