Load content to buffer before attempting deserialization #1705
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Fixes issue #1384 by ensuring response content is loaded to a buffer before attempting serialization.
What is the current behavior?
When an exception is thrown during deserialization,
ApiException
attempts to capture the request content to theContent
property to make it available when the exception is being handled (e.g. so apps can log the content to aid debugging). However, the underlying stream in theHttpContent
object has already been consumed by the deserialization and cannot be re-read.What is the new behavior?
By loading the content to a buffer before attempting to deserialize we ensure the stream can be re-read by
ApiException
.What might this PR break?
I've moved the
LoadIntoBufferAsync
call to just before the call to the (de)serializer to ensure we only do this on this exact code path, rather than as soon as thecontent
variable is set as I originally suggested in #1384. This should avoid any problems that could arise from API calls that expect to handle theHttpContent
orStream
directly (callingLoadIntoBufferAsync
is probably not desirable for these scenarios where we might expect endpoints to return long-lived connections or very large payloads).Please check if the PR fulfills these requirements
Other information: