Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0] Update HttpClientJsonExtensions.Get.AsyncEnumerable.cs #92199

Merged
merged 8 commits into from
Sep 18, 2023
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; }
}
Loading