tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET

Home Page:https://flurl.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UseNewtonsoft has no effect on clientless pattern

Dreamescaper opened this issue · comments

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.

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.

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.

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