tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET
https://flurl.dev
MIT License
4.23k stars 387 forks source link

UseNewtonsoft has no effect on clientless pattern #797

Closed Dreamescaper closed 10 months ago

Dreamescaper commented 10 months ago

I'm using clientless pattern, and I plan to use Newtonsoft.Json as for now. Unfortunately, I cannot find a way to use Newtonsoft serializer for clientless pattern by default.

I use the following code to set the defaults:

            FlurlHttp.Clients.WithDefaults(d =>
            {
                d.UseNewtonsoft();
            });

But it is not used by default, the code below still uses DefaultJsonSerializer.

await "https://my.test/".PostJsonAsync(body);

Seems like the problem is that the client from FlurlHttp.Clients is assigned to FlurlRequest instance after the body is serialized - and therefore NewtonsoftSerializer is not used.

I assume it also affects any other Serializer customizations.

Dreamescaper commented 10 months ago

I was able to workaround it by adding the following line to my setup code:

            var defaultSettings = (FlurlHttpSettings)typeof(FlurlHttpSettings).GetField("Defaults", BindingFlags.NonPublic | BindingFlags.Static)!.GetValue(null)!;
            defaultSettings.JsonSerializer = new NewtonsoftJsonSerializer();

But that doesn't look like an expected solution.

tmenier commented 10 months ago

Yep, this is a pretty blatant bug. Thanks for the details, but it's actually a duplicate of #783 which I'm working on now.

tmenier commented 10 months ago

Fixed in the latest release. (See #783.) Thanks again for providing details, it was helpful.