Skip to content

Commit

Permalink
Load content to buffer before attempting deserialization (#1705)
Browse files Browse the repository at this point in the history
* Load content to buffer before attempting deserialization to ensure raw content can be captured in ApiException if an exception occurs during deserialization

* Ensure stream is seekable before attempting to load into buffer

---------

Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
  • Loading branch information
LichP and ChrisPulman committed Jun 3, 2024
1 parent b40dfaf commit d85edef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions Refit.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ public sealed class ThrowOnGetLengthMemoryStream : MemoryStream
{
public bool CanGetLength { get; set; }

public override bool CanSeek { get => CanGetLength; }
public override long Length =>
CanGetLength ? base.Length : throw new NotSupportedException();
}
7 changes: 7 additions & 0 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ CancellationToken cancellationToken
}
else
{
using var stream = await content
.ReadAsStreamAsync(cancellationToken)
.ConfigureAwait(false);
if (stream.CanSeek)
{
await content.LoadIntoBufferAsync().ConfigureAwait(false);
}
result = await serializer
.FromHttpContentAsync<T>(content, cancellationToken)
.ConfigureAwait(false);
Expand Down

0 comments on commit d85edef

Please sign in to comment.