Skip to content

Commit

Permalink
Update HttpClientJsonExtensions.Get.AsyncEnumerable.cs (dotnet#92180)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* refine updates

---------

Co-authored-by: Eirik Tsarpalis <[email protected]>
  • Loading branch information
WeihanLi and eiriktsarpalis authored Sep 17, 2023
1 parent 51a694b commit 353d5ea
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 353d5ea

Please sign in to comment.