Skip to content

Commit e00e6aa

Browse files
github-actions[bot]WeihanLieiriktsarpalis
authored
[release/8.0] Update HttpClientJsonExtensions.Get.AsyncEnumerable.cs (#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 <[email protected]> * refine updates --------- Co-authored-by: Weihan Li <[email protected]> Co-authored-by: Eirik Tsarpalis <[email protected]>
1 parent 1cbf763 commit e00e6aa

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpClientJsonExtensions.Get.AsyncEnumerable.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ public static partial class HttpClientJsonExtensions
135135
JsonSerializerOptions? options,
136136
CancellationToken cancellationToken)
137137
{
138-
options ??= JsonSerializerOptions.Default;
139-
options.MakeReadOnly();
140-
141-
var jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfo(typeof(TValue));
138+
var jsonTypeInfo = (JsonTypeInfo<TValue>)JsonHelpers.GetJsonTypeInfo(typeof(TValue), options);
142139

143140
return FromJsonStreamAsyncCore(client, requestUri, jsonTypeInfo, cancellationToken);
144141
}

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

+42
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,43 @@ public async Task GetFromJsonAsAsyncEnumerable_EnforcesTimeoutOnInitialRequest()
580580
await Task.Delay(TimeSpan.FromMilliseconds(10));
581581
}
582582
}
583+
584+
[Fact]
585+
public async Task GetFromJsonAsAsyncEnumerable_SerializerUsesCamelCase()
586+
{
587+
using var client = new HttpClient(new CustomResponseHandler((r, c) =>
588+
{
589+
string json = """[{"value":1},{"value":2}]""";
590+
HttpResponseMessage response = new()
591+
{
592+
Content = new StringContent(json)
593+
};
594+
return Task.FromResult(response);
595+
}));
596+
597+
await foreach (var m in client.GetFromJsonAsAsyncEnumerable<TestModel>("http://dummyUrl"))
598+
{
599+
Assert.True(m.Value > 0);
600+
}
601+
}
602+
603+
[Fact]
604+
public async Task GetFromJsonAsAsyncEnumerable_CustomSerializerOptions()
605+
{
606+
using var client = new HttpClient(new CustomResponseHandler((r, c) =>
607+
{
608+
string json = """[{"Value":1},{"Value":2}]""";
609+
HttpResponseMessage response = new()
610+
{
611+
Content = new StringContent(json)
612+
};
613+
return Task.FromResult(response);
614+
}));
615+
await foreach (var m in client.GetFromJsonAsAsyncEnumerable<TestModel>("http://dummyUrl", JsonSerializerOptions.Default))
616+
{
617+
Assert.True(m.Value > 0);
618+
}
619+
}
583620
}
584621
}
585622

@@ -593,3 +630,8 @@ public CustomResponseHandler(
593630
protected override Task<HttpResponseMessage> SendAsync(
594631
HttpRequestMessage request, CancellationToken cancellationToken) => _func(request, cancellationToken);
595632
}
633+
634+
file sealed class TestModel
635+
{
636+
public int Value { get; set; }
637+
}

0 commit comments

Comments
 (0)