From 8956de98db265061e1ef48fe365ef092409941fd Mon Sep 17 00:00:00 2001 From: hongxiang Date: Fri, 1 Apr 2022 09:44:58 +0800 Subject: [PATCH 1/2] make ResponseDecoder return nullable String type since the usage is return String? --- dio/lib/src/options.dart | 2 +- dio/lib/src/transformer.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dio/lib/src/options.dart b/dio/lib/src/options.dart index 5da0bbe41..373452d25 100644 --- a/dio/lib/src/options.dart +++ b/dio/lib/src/options.dart @@ -72,7 +72,7 @@ enum ListFormat { typedef ValidateStatus = bool Function(int? status); -typedef ResponseDecoder = String Function( +typedef ResponseDecoder = String? Function( List responseBytes, RequestOptions options, ResponseBody responseBody); typedef RequestEncoder = List Function( String request, RequestOptions options); diff --git a/dio/lib/src/transformer.dart b/dio/lib/src/transformer.dart index 79062a4dd..534735f9a 100644 --- a/dio/lib/src/transformer.dart +++ b/dio/lib/src/transformer.dart @@ -163,7 +163,7 @@ class DefaultTransformer extends Transformer { } else { responseBody = utf8.decode(responseBytes, allowMalformed: true); } - if (responseBody.isNotEmpty && + if (responseBody !=null && responseBody.isNotEmpty && options.responseType == ResponseType.json && Transformer.isJsonMimeType( response.headers[Headers.contentTypeHeader]?.first)) { From 40f8a3b5baf1a3466d123d6accc3bc6f5fe8347d Mon Sep 17 00:00:00 2001 From: hongxiang Date: Thu, 20 Oct 2022 17:56:34 +0800 Subject: [PATCH 2/2] add test for ResponseDecoder that return nullable String --- dio/lib/src/transformer.dart | 5 +++-- dio/test/options_test.dart | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dio/lib/src/transformer.dart b/dio/lib/src/transformer.dart index 534735f9a..25b038751 100644 --- a/dio/lib/src/transformer.dart +++ b/dio/lib/src/transformer.dart @@ -160,10 +160,11 @@ class DefaultTransformer extends Transformer { options, response..stream = Stream.empty(), ); - } else { + } else if (responseBytes.isNotEmpty) { responseBody = utf8.decode(responseBytes, allowMalformed: true); } - if (responseBody !=null && responseBody.isNotEmpty && + if (responseBody != null && + responseBody.isNotEmpty && options.responseType == ResponseType.json && Transformer.isJsonMimeType( response.headers[Headers.contentTypeHeader]?.first)) { diff --git a/dio/test/options_test.dart b/dio/test/options_test.dart index 1322a9ce1..ee396e75a 100644 --- a/dio/test/options_test.dart +++ b/dio/test/options_test.dart @@ -270,4 +270,15 @@ void main() { assert(r3.uri.toString() == 'https://www.example.com/test'); assert(r3.headers[Headers.contentTypeHeader] == null); }); + + test("#test responseDecoder return null", () async { + final dio = Dio(); + dio.options.responseDecoder = (_,__,___) => null; + dio.options.baseUrl = EchoAdapter.mockBase; + dio.httpClientAdapter = EchoAdapter(); + + final Response response = await dio.get(""); + + expect(response.data, null); + }); }