Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/FluentHttpClient.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace FluentHttpClient.Tests;

public class IntegrationTests
{
[Fact]
public async Task ReadJsonAsync_ShouldReturnDeserializedObjectMultipleTimes()
{
var client = new HttpClient
{
BaseAddress = new Uri("https://jsonplaceholder.typicode.com")
};

var response = await client
.UsingRoute("posts/1")
.GetAsync();

var post1 = await response.ReadJsonAsync<Post>();
var post2 = await response.ReadJsonAsync<Post>();

post1.ShouldNotBeNull();
post2.ShouldNotBeNull();
}
}

public sealed class Post
{
public int Id { get; set; }
public int UserId { get; set; }
public string? Title { get; set; }
public string? Body { get; set; }
}
10 changes: 5 additions & 5 deletions src/FluentHttpClient/FluentJsonDeserialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ public static class FluentJsonDeserialization
}

#if NET5_0_OR_GREATER
var stream = await response.Content
.ReadAsStreamAsync(cancellationToken)
var json = await response.Content
.ReadAsStringAsync(cancellationToken)
.ConfigureAwait(false);
#else
var stream = await response.Content
.ReadAsStreamAsync()
var json = await response.Content
.ReadAsStringAsync()
.ConfigureAwait(false);
#endif

return JsonDocument.Parse(stream, documentOptions);
return JsonDocument.Parse(json, documentOptions);
}

/// <summary>
Expand Down
16 changes: 7 additions & 9 deletions src/FluentHttpClient/FluentJsonTypedDeserialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,23 @@ public static partial class FluentJsonTypedDeserialization
// NOTE: HttpResponseMessage.Content is never null on modern TFMs, but can be on older platforms.
// These checks exist for cross-target safety and are not hit in current test runs.
if (response.Content is null)
{
return default;
}

options ??= FluentJsonSerializer.DefaultJsonSerializerOptions;

#if NET5_0_OR_GREATER
var stream = await response.Content
.ReadAsStreamAsync(cancellationToken)
var json = await response.Content
.ReadAsStringAsync(cancellationToken)
.ConfigureAwait(false);
#else
var stream = await response.Content
.ReadAsStreamAsync()
var json = await response.Content
.ReadAsStringAsync()
.ConfigureAwait(false);
#endif

return await JsonSerializer
.DeserializeAsync<T>(stream, options, cancellationToken)
.ConfigureAwait(false);
return cancellationToken.IsCancellationRequested
? throw new OperationCanceledException(cancellationToken)
: JsonSerializer.Deserialize<T>(json, options);
}

/// <summary>
Expand Down