From 0687488de3d198a3f66321570d0094b1cf27a4be Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 22 Sep 2023 09:55:08 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Avoid=20`Interceptors.clear`=20r?= =?UTF-8?q?emoves=20`ImplyContentTypeInterceptor`=20(#1975)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves #1973 ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [x] I have added the required tests to prove the fix/feature I'm adding - [x] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package ### Additional context and info (if any) The PR only addresses `clear`. However, lists can be modified in various ways which might also remove/replace the interceptor. We might need to rethink an extra internal interceptors list. --- dio/CHANGELOG.md | 1 + dio/lib/src/interceptor.dart | 11 +++++++++++ dio/test/interceptor_test.dart | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index bf2f3a650..f3df5e24e 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -12,6 +12,7 @@ See the [Migration Guide][] for the complete breaking changes list.** - Reduce cases in which browsers would trigger a CORS preflight request. - Add warnings in debug mode when using `sendTimeout` and `onSendProgress` with an empty request body. - Fix `receiveTimeout` not working correctly on web. +- Fix `ImplyContentTypeInterceptor` can be removed by `Interceptors.clear()` by default. ## 5.3.2 diff --git a/dio/lib/src/interceptor.dart b/dio/lib/src/interceptor.dart index ec1927c20..05c9c11c7 100644 --- a/dio/lib/src/interceptor.dart +++ b/dio/lib/src/interceptor.dart @@ -319,6 +319,17 @@ class Interceptors extends ListMixin { } } + /// The default [ImplyContentTypeInterceptor] will be removed only if + /// [keepImplyContentTypeInterceptor] is false. + @override + void clear({bool keepImplyContentTypeInterceptor = true}) { + if (keepImplyContentTypeInterceptor) { + _list.removeWhere((e) => e is! ImplyContentTypeInterceptor); + } else { + super.clear(); + } + } + /// Remove the default imply content type interceptor. void removeImplyContentTypeInterceptor() { _list.removeWhere((e) => e is ImplyContentTypeInterceptor); diff --git a/dio/test/interceptor_test.dart b/dio/test/interceptor_test.dart index 80a5abb11..eeed6baa4 100644 --- a/dio/test/interceptor_test.dart +++ b/dio/test/interceptor_test.dart @@ -583,6 +583,7 @@ void main() { expect(tokenRequestCounts, 1); expect(result, 3); expect(myInter.requestCount, predicate((int e) => e > 0)); + // The `ImplyContentTypeInterceptor` will be replaced. dio.interceptors[0] = myInter; dio.interceptors.clear(); expect(dio.interceptors.isEmpty, true); @@ -659,6 +660,9 @@ void main() { expect(interceptors.length, equals(2)); expect(interceptors, isNotEmpty); interceptors.clear(); + expect(interceptors.length, equals(1)); + expect(interceptors.single, isA()); + interceptors.clear(keepImplyContentTypeInterceptor: false); expect(interceptors.length, equals(0)); expect(interceptors, isEmpty); });