Skip to content

Commit

Permalink
Fix: Resolve ListParam equality issue for caching identical requests (#…
Browse files Browse the repository at this point in the history
…2366)

### What this PR does

Resolves #2364

This PR addresses an issue with ListParam equality, where identical
requests were not recognized as the same due to reference comparison of
lists. This caused multiple identical requests to be sent to the server,
even when caching was implemented.

### Changes introduced

Updated ListParam's operator == to use DeepCollectionEquality for proper
deep equality comparison of list contents.
Added relevant unit tests to validate the fix and ensure no regression.
  • Loading branch information
suojae authored Jan 28, 2025
1 parent e88e86f commit b58b440
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
- Removes redundant warnings when composing request options on Web.
- Fixes boundary inconsistency in `FormData.clone()`.
- Support `FileAccessMode` in `Dio.download` and `Dio.downloadUri` to change download file opening mode
- Fix `ListParam` equality by using the `DeepCollectionEquality`.

## 5.7.0

Expand Down
9 changes: 7 additions & 2 deletions dio/lib/src/parameter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:collection/collection.dart';

import 'options.dart';

/// Indicates a param being used as queries or form data,
Expand Down Expand Up @@ -26,9 +28,12 @@ class ListParam<T> {
identical(this, other) ||
other is ListParam &&
runtimeType == other.runtimeType &&
value == other.value &&
const DeepCollectionEquality().equals(value, other.value) &&
format == other.format;

@override
int get hashCode => value.hashCode ^ format.hashCode;
int get hashCode => Object.hash(
const DeepCollectionEquality().hash(value),
format,
);
}
1 change: 1 addition & 0 deletions dio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ environment:

dependencies:
async: ^2.8.2
collection: ^1.16.0
http_parser: ^4.0.0
meta: ^1.5.0
path: ^1.8.0
Expand Down
24 changes: 24 additions & 0 deletions dio/test/parameter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:dio/dio.dart';
import 'package:test/test.dart';

void main() {
group('ListParam', () {
test('param1 and param2 should be considered equal', () {
final param1 = const ListParam(['item1', 'item2'], ListFormat.csv);
final param2 = const ListParam(['item1', 'item2'], ListFormat.csv);
expect(param1 == param2, isTrue);
});

test('param1 and param3 should not be considered equal', () {
final param1 = const ListParam(['item1', 'item2'], ListFormat.csv);
final param3 = const ListParam(['item3', 'item4'], ListFormat.csv);
expect(param1 == param3, isFalse);
});

test('Order matters: param1 and param4 should not be equal', () {
final param1 = const ListParam(['item1', 'item2'], ListFormat.csv);
final param4 = const ListParam(['item2', 'item1'], ListFormat.csv);
expect(param1 == param4, isFalse);
});
});
}

0 comments on commit b58b440

Please sign in to comment.