Skip to content

Commit

Permalink
[release/8.0] Update HttpClientJsonExtensions.Get.AsyncEnumerable.cs (d…
Browse files Browse the repository at this point in the history
…otnet#92199)

* Update HttpClientJsonExtensions.Get.AsyncEnumerable.cs

reuse method from ReadFromJsonAsAsyncEnumerable

* fix trailing space

* feat: add test case

* Update HttpClientJsonExtensions.Get.AsyncEnumerable.cs

add httpClient null check

* Update HttpClientJsonExtensions.Get.AsyncEnumerable.cs

fix trailing space style

* Update src/libraries/System.Net.Http.Json/tests/FunctionalTests/HttpClientJsonExtensionsTests.cs

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>

* refine updates

---------

Co-authored-by: Weihan Li <weihanli@outlook.com>
Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
  • Loading branch information
3 people committed Sep 18, 2023
1 parent 1cbf763 commit e00e6aa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ public static partial class HttpClientJsonExtensions
JsonSerializerOptions? options,
CancellationToken cancellationToken)
{
options ??= JsonSerializerOptions.Default;
options.MakeReadOnly();

var jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfo(typeof(TValue));
var jsonTypeInfo = (JsonTypeInfo<TValue>)JsonHelpers.GetJsonTypeInfo(typeof(TValue), options);

return FromJsonStreamAsyncCore(client, requestUri, jsonTypeInfo, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,43 @@ public async Task GetFromJsonAsAsyncEnumerable_EnforcesTimeoutOnInitialRequest()
await Task.Delay(TimeSpan.FromMilliseconds(10));
}
}

[Fact]
public async Task GetFromJsonAsAsyncEnumerable_SerializerUsesCamelCase()
{
using var client = new HttpClient(new CustomResponseHandler((r, c) =>
{
string json = """[{"value":1},{"value":2}]""";
HttpResponseMessage response = new()
{
Content = new StringContent(json)
};
return Task.FromResult(response);
}));

await foreach (var m in client.GetFromJsonAsAsyncEnumerable<TestModel>("http://dummyUrl"))
{
Assert.True(m.Value > 0);
}
}

[Fact]
public async Task GetFromJsonAsAsyncEnumerable_CustomSerializerOptions()
{
using var client = new HttpClient(new CustomResponseHandler((r, c) =>
{
string json = """[{"Value":1},{"Value":2}]""";
HttpResponseMessage response = new()
{
Content = new StringContent(json)
};
return Task.FromResult(response);
}));
await foreach (var m in client.GetFromJsonAsAsyncEnumerable<TestModel>("http://dummyUrl", JsonSerializerOptions.Default))
{
Assert.True(m.Value > 0);
}
}
}
}

Expand All @@ -593,3 +630,8 @@ public CustomResponseHandler(
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken) => _func(request, cancellationToken);
}

file sealed class TestModel
{
public int Value { get; set; }
}

0 comments on commit e00e6aa

Please sign in to comment.