Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
8 changes: 5 additions & 3 deletions packages/dart/lib/src/objects/parse_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ class _ParseArray implements _Valuable<List>, _ParseSaveStateAwareChild {
@mustCallSuper
void onSaved() {
setMode = false;
_savedArray.clear();
_savedArray.addAll(_estimatedArrayBeforeSaving ?? []);
_estimatedArrayBeforeSaving = null;
if (_estimatedArrayBeforeSaving != null) {
_savedArray.clear();
_savedArray.addAll(_estimatedArrayBeforeSaving!);
_estimatedArrayBeforeSaving = null;
}

if (_lastPreformedOperationBeforeSaving == lastPreformedOperation) {
// No operations were performed during the save process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,78 @@ void main() {
expect(listValue, orderedEquals([1, 2]));
});

test('Arrays should not be cleared when calling clearUnsavedChanges() '
'after receiving response from fetch/query (issue #1038)', () async {
// arrange - First, save an object with an array to create a _ParseArray
dietPlansObject.setAdd(keyArray, 1);
dietPlansObject.setAdd(keyArray, 2);

when(
client.post(any, options: anyNamed("options"), data: anyNamed('data')),
).thenAnswer(
(_) async => ParseNetworkResponse(
statusCode: 200,
data: jsonEncode({
"objectId": "Mn1iJTkWTE",
"createdAt": "2023-03-05T00:25:31.466Z",
}),
),
);

await dietPlansObject
.save(); // This creates the _ParseArray and calls onSaving/onSaved

// Now set up a mock fetch response that returns updated array data
final getPath = Uri.parse(
'$serverUrl$keyEndPointClasses${dietPlansObject.parseClassName}/${dietPlansObject.objectId}',
).toString();

const resultsFromServer = {
"results": [
{
"objectId": "Mn1iJTkWTE",
keyArray: [
1,
2,
3,
], // Server now has an updated array with one more item
"createdAt": "2023-03-05T00:25:31.466Z",
"updatedAt": "2023-03-05T00:25:31.466Z",
},
],
};

when(
client.get(
getPath,
options: anyNamed("options"),
onReceiveProgress: anyNamed("onReceiveProgress"),
),
).thenAnswer(
(_) async => ParseNetworkResponse(
statusCode: 200,
data: jsonEncode(resultsFromServer),
),
);

// act - Fetch the object from server to get the updated array
// This simulates the scenario from issue #1038 where after fetching/querying
// an object (e.g., via getUpdatedUser() or fetch()), calling clearUnsavedChanges()
// would incorrectly clear the arrays.
ParseObject fetchedObject = await dietPlansObject.fetch();

// Verify array is populated correctly from the fetch response
expect(fetchedObject.get(keyArray), orderedEquals([1, 2, 3]));

// Now clear unsaved changes - this should NOT clear the arrays
// Before the fix (PR #1039), this would set arrays to empty
fetchedObject.clearUnsavedChanges();

// assert - Arrays should still have their values from the server
final listValue = fetchedObject.get(keyArray);
expect(listValue, orderedEquals([1, 2, 3]));
});

test('The list value and the value for api request should be identical '
'before and after the save() failed to save the object', () async {
// arrange
Expand Down