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: Resolve ListParam equality issue for caching identical requests #2366

Merged
merged 6 commits into from
Jan 28, 2025
Merged
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
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);
});
});
}
Loading