Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@KodamaSakuno
Copy link
Contributor

What kind of change does this PR introduce?
Correct the wrong example about JSON source generator in the readme.

Other information:
The following code shows the difference.

public class UnitTest
{
    private readonly HttpClient _client = new(new MyHttpMessageHandler());

    [Fact]
    public async Task Test1()
    {
        Response? response = null;
        
        var options = MyJsonSerializerContext.Default.Options;
        
        var exception = await Record.ExceptionAsync(async () =>
        {
            response = await _client.GetFromJsonAsync<Response>("http://abc", options);
        });
        
        Assert.Null(exception);
        Assert.Equal(1234, response?.Code);
    }
    
    [Fact]
    public async Task Test2()
    {
        Response? response = null;
        
        var options = new JsonSerializerOptions()
        {
            TypeInfoResolver = MyJsonSerializerContext.Default,
        };
        
        var exception = await Record.ExceptionAsync(async () =>
        {
            response = await _client.GetFromJsonAsync<Response>("http://abc", options);
        });
        
        Assert.IsType<JsonException>(exception);
        Assert.StartsWith("The JSON value could not be converted to System.Int32. Path: $.Code", exception.Message);
    }
}

public class MyHttpMessageHandler : HttpMessageHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return Task.FromResult(new HttpResponseMessage()
        {
            Content = new StringContent("{\"Code\": \"1234\"}"),
        });
    }
}
    
public class Response
{
    public int Code { get; set; }
}

[JsonSourceGenerationOptions(NumberHandling = JsonNumberHandling.AllowReadingFromString)]
[JsonSerializable(typeof(Response))]
public partial class MyJsonSerializerContext : JsonSerializerContext;

@ChrisPulman ChrisPulman enabled auto-merge (squash) May 2, 2025 19:29
@ChrisPulman ChrisPulman merged commit f599693 into reactiveui:main May 2, 2025
1 check passed
@codecov
Copy link

codecov bot commented May 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.03%. Comparing base (6ebeda5) to head (28acb43).
Report is 190 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1983      +/-   ##
==========================================
- Coverage   87.73%   83.03%   -4.71%     
==========================================
  Files          33       37       +4     
  Lines        2348     2640     +292     
  Branches      294      380      +86     
==========================================
+ Hits         2060     2192     +132     
- Misses        208      362     +154     
- Partials       80       86       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants