From 22fdb85af331daf6e28d440e04b52bc941fa53e6 Mon Sep 17 00:00:00 2001 From: Peter Leibiger Date: Sun, 11 Jun 2023 01:04:41 +0200 Subject: [PATCH] Revert changes to List body data handling (#1860) A previous change to improve the performance of binary uploads accidentally broke the functionality of uploading `List` json bodies. Fixes #1858 --- dio/CHANGELOG.md | 5 ++++- dio/lib/src/dio_mixin.dart | 3 --- dio/test/upload_test.dart | 36 ++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 8f3a91dab..2a3770f0a 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -7,6 +7,10 @@ See the [Migration Guide][] for the complete breaking changes list.** *None.* +## 5.2.0+2 + +- Revert changes to handling of `List` body data. + ## 5.2.0+1 - Fix `DioErrorType` deprecation hint. @@ -20,7 +24,6 @@ See the [Migration Guide][] for the complete breaking changes list.** Dio 6.0.0 - Please use the replacement `IOHttpClientAdapter.createHttpClient` instead. - Using `CancelToken` no longer closes and re-creates `HttpClient` for each request when `IOHttpClientAdapter` is used. - Fix timeout handling for browser `receiveTimeout`. -- Using `CancelToken` no longer closes and re-creates `HttpClient` for each request when `IOHttpClientAdapter` is used. - Improve performance when sending binary data (`List`/`Uint8List`). ## 5.1.2 diff --git a/dio/lib/src/dio_mixin.dart b/dio/lib/src/dio_mixin.dart index 3d51e8a0b..93966a199 100644 --- a/dio/lib/src/dio_mixin.dart +++ b/dio/lib/src/dio_mixin.dart @@ -640,9 +640,6 @@ abstract class DioMixin implements Dio { if (data is Uint8List) { // Handle binary data which does not need to be transformed bytes = data; - } else if (data is List) { - // Handle binary data which does not need to be transformed - bytes = Uint8List.fromList(data); } else { // Call request transformer for anything else final transformed = await transformer.transformRequest(options); diff --git a/dio/test/upload_test.dart b/dio/test/upload_test.dart index 956034e18..da3d51118 100644 --- a/dio/test/upload_test.dart +++ b/dio/test/upload_test.dart @@ -1,13 +1,11 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'dart:typed_data'; import 'package:dio/dio.dart'; -import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; -import 'mock/http_mock.mocks.dart'; - void main() { late Dio dio; @@ -15,20 +13,28 @@ void main() { dio = Dio()..options.baseUrl = 'https://httpbun.com/'; }); - test('binary data should not be transformed', () async { - final bytes = List.generate(1024, (index) => index); - final transformer = MockTransformer(); - when(transformer.transformResponse(any, any)).thenAnswer( - (i) => i.positionalArguments[1], - ); + test('Uint8List should not be transformed', () async { + final bytes = Uint8List.fromList(List.generate(10, (index) => index)); + final transformer = dio.transformer = _TestTransformer(); final r = await dio.put( '/put', data: bytes, ); - verifyNever(transformer.transformRequest(any)); + expect(transformer.requestTransformed, isFalse); expect(r.statusCode, 200); }); + test('List should be transformed', () async { + final ints = List.generate(10, (index) => index); + final transformer = dio.transformer = _TestTransformer(); + final r = await dio.put( + '/put', + data: ints, + ); + expect(transformer.requestTransformed, isTrue); + expect(r.data['data'], ints.toString()); + }); + test('stream', () async { const str = 'hello 😌'; final bytes = utf8.encode(str).toList(); @@ -96,3 +102,13 @@ void main() { testOn: 'vm', ); } + +class _TestTransformer extends BackgroundTransformer { + bool requestTransformed = false; + + @override + Future transformRequest(RequestOptions options) async { + requestTransformed = true; + return super.transformRequest(options); + } +}