-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve fallback to null on empty responses (#2285)
fixes #2279 This improves handling of the following case: - ResponseType is json - Server Content-Type is json - Server Content-Length is nonzero - Response Body is empty This can happen in some cases according to HTTP spec (e.g. HEAD requests), but some servers return responses like this (see #2279) also in other cases, even if it violates the HTTP spec. With #2250, in some of these cases dio would throw an exception, which caused a regression for some users; see #2279 I do believe that dio should handle these cases for gracefully, This PR brings back the previous behavior of returning null. ### 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) <!-- Provide more context and info about the PR. --> --------- Signed-off-by: Martin Kamleithner <[email protected]> Co-authored-by: Jonas Uekötter <[email protected]>
- Loading branch information
1 parent
679a144
commit d7cad71
Showing
4 changed files
with
103 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
dio/lib/src/transformers/util/transform_empty_to_null.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'dart:async'; | ||
import 'dart:typed_data'; | ||
|
||
/// A [StreamTransformer] that replaces an empty stream of Uint8List with a default value | ||
/// - the utf8-encoded string "null". | ||
/// Feeding an empty stream to a JSON decoder will throw an exception, so this transformer | ||
/// is used to prevent that; the JSON decoder will instead return null. | ||
class DefaultNullIfEmptyStreamTransformer | ||
extends StreamTransformerBase<Uint8List, Uint8List> { | ||
const DefaultNullIfEmptyStreamTransformer(); | ||
|
||
@override | ||
Stream<Uint8List> bind(Stream<Uint8List> stream) { | ||
return Stream.eventTransformed( | ||
stream, | ||
(sink) => _DefaultIfEmptyStreamSink(sink), | ||
); | ||
} | ||
} | ||
|
||
class _DefaultIfEmptyStreamSink implements EventSink<Uint8List> { | ||
_DefaultIfEmptyStreamSink(this._outputSink); | ||
|
||
/// Hard-coded constant for replacement value, "null" | ||
static final Uint8List _nullUtf8Value = | ||
Uint8List.fromList(const [110, 117, 108, 108]); | ||
|
||
final EventSink<Uint8List> _outputSink; | ||
bool _hasData = false; | ||
|
||
@override | ||
void add(Uint8List data) { | ||
_hasData = _hasData || data.isNotEmpty; | ||
_outputSink.add(data); | ||
} | ||
|
||
@override | ||
void addError(e, [st]) => _outputSink.addError(e, st); | ||
|
||
@override | ||
void close() { | ||
if (!_hasData) { | ||
_outputSink.add(_nullUtf8Value); | ||
} | ||
|
||
_outputSink.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters