Skip to content
Open
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
5 changes: 4 additions & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ See the [Migration Guide][] for the complete breaking changes list.**

## Unreleased

*None.*
- Fix `FusedTransformer` (the default transformer) throwing a `FormatException`
on an empty response body when a custom `responseDecoder` is set. It now
returns an empty result, consistent with `SyncTransformer` and
`BackgroundTransformer`.

## 5.10.0

Expand Down
4 changes: 3 additions & 1 deletion dio/lib/src/transformers/fused_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class FusedTransformer extends Transformer {
decodedResponse = null;
}

if (isJsonContent && decodedResponse != null) {
if (isJsonContent &&
decodedResponse != null &&
decodedResponse.isNotEmpty) {
// slow path decoder, since there was a custom decoder specified
return jsonDecode(decodedResponse);
} else if (customResponseDecoder != null) {
Expand Down
28 changes: 28 additions & 0 deletions dio/test/transformer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,34 @@ void main() {
expect(jsonResponse, null);
});

test(
'empty JSON body with a custom responseDecoder does not throw',
() async {
// A custom decoder that returns the utf8-decoded body, which is an
// empty string for an empty response. Without guarding against an
// empty decoded string, the fused transformer would feed '' into
// jsonDecode and throw a FormatException, unlike SyncTransformer and
// BackgroundTransformer which both return an empty string here.
String decoder(List<int> bytes, RequestOptions o, ResponseBody rb) =>
utf8.decode(bytes, allowMalformed: true);
final transformer = FusedTransformer();
final response = await transformer.transformResponse(
RequestOptions(
responseType: ResponseType.json,
responseDecoder: decoder,
),
ResponseBody.fromBytes(
[],
200,
headers: {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
),
);
expect(response, '');
},
);

test('transform the request using urlencode', () async {
final transformer = FusedTransformer();

Expand Down
Loading