From d85edefb46bdb3ed24d81bafb463fa7bfca5df91 Mon Sep 17 00:00:00 2001 From: Phil Stewart Date: Mon, 3 Jun 2024 20:42:36 +0100 Subject: [PATCH] Load content to buffer before attempting deserialization (#1705) * 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 --- Refit.Tests/ResponseTests.cs | 1 + Refit/RequestBuilderImplementation.cs | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/Refit.Tests/ResponseTests.cs b/Refit.Tests/ResponseTests.cs index 0d57e8761..0b3136968 100644 --- a/Refit.Tests/ResponseTests.cs +++ b/Refit.Tests/ResponseTests.cs @@ -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(); } diff --git a/Refit/RequestBuilderImplementation.cs b/Refit/RequestBuilderImplementation.cs index 20092b544..8c1780977 100644 --- a/Refit/RequestBuilderImplementation.cs +++ b/Refit/RequestBuilderImplementation.cs @@ -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(content, cancellationToken) .ConfigureAwait(false);