-
-
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
fix: improve fallback to null on empty responses #2285
Conversation
Co-authored-by: Jonas Uekötter <[email protected]> Signed-off-by: Martin Kamleithner <[email protected]>
…com/knaeckeKami/dio into fix/fused_transformer_null_fallback
This is also a problem downstream in |
@kuhnroyal @AlexV525 Do we want to release this as |
I'd argue it's a bug fix, as the change occurred with #2250, and this restores the old behavior. But of course, a bug fix can also be viewed as a breaking change if people depend on it ;) |
Yeah, I'm tempted to release it as |
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.
I didn't take a detailed look into what happened. In general we need to provide tests that can pass in previous specific versions and with the request changes.
dio/CHANGELOG.md
Outdated
- Graceful handling of Responses with nonzero Content-Length, Content-Type json, but empty body | ||
- Empty responses are now transformed to `null` |
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
/// Hard-coded constant for replacement value, "null" | ||
static final Uint8List _nullUtf8Value = | ||
Uint8List.fromList(const [110, 117, 108, 108]); |
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.
What does the value help with?
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.
It's for adding the byte sequence for "null" to the response stream, in the case that the response stream is unexpectedly empty.
So we return null
instead of throwing an exception.
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.
Wouldn't that cause a side effect that user won't understand why their response becomes "null"
?
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.
For clarification: The response won't become the String "null"
, it would become the value null
after the json decoder decodes that byte sequence.
It has always been the behavior of Dio to return null
when the ResponseType
is .json
and the response is empty.
Only in the recent release with the FusedTransformer
there is now a corner case that if
- the
ResponseType
is.json
and - the
Content-Type
header isapplication/json
and - the
Content-Length
header has a value of > 0 and - the response is still empty
That an exception is thrown (instead of the old behavior of returning null).
This is because we currently assume that if the Content-Length
is >0
, that the response body will not be empty. So the response stream is fed into the json decoder, but the decoder throws on empty streams.
This transformation is not applied for other ResponseType
than .json
.
I would also be okay with removing the behavior of returning null on empty responses when the ResponseType
is .json
but the body is empty completely, and always throw an exception in that case.
But that would be a breaking change for sure.
The behavior of the current release, where it mostly returns null
on empty responses but throws an exception in certain corner cases (mostly responses that are invalid according to the HTTP spec, but still occur in the real world), is not ideal IMO.
I added a test here: This test would pass before #2250, and after this PR is merged. A user opened #2279 because of that. The server sends an invalid response (according to the HTTP spec), but I still think that it's better to handle such cases gracefully in an HTTP client (especially if the old behavior was graceful (return |
Signed-off-by: Martin Kamleithner <[email protected]>
Code Coverage Report: Only Changed Files listed
Minimum allowed coverage is |
fixes #2279
This improves handling of the following case:
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
main
branch to avoid conflicts (via merge from master or rebase)CHANGELOG.md
in the corresponding packageAdditional context and info (if any)