-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: improve fallback to null on empty responses #2285
Changes from 6 commits
f73ff71
79d88f4
9c02075
61ad86e
eac00c9
0f9dcff
1591321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]); | ||
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the value help with? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's for adding the byte sequence for "null" to the response stream, in the case that the response stream is unexpectedly empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't that cause a side effect that user won't understand why their response becomes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarification: The response won't become the String It has always been the behavior of Dio to return
That an exception is thrown (instead of the old behavior of returning null). This transformation is not applied for other I would also be okay with removing the behavior of returning null on empty responses when the The behavior of the current release, where it mostly returns |
||
|
||
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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two entries? Also please write words in correct capitalization: response, content-type, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first line are the affected cases, the second line describes the result
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed capitalization of "Responses".
The capitalization of the headers are like in in the official spec like https://datatracker.ietf.org/doc/html/rfc2616#section-14.13
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length