RestClient configured with Newtonsoft.Json serializer still instantiates System.Text.Json
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 65/100
Direzione di ricerca
Inizia con RestClient.ConfigureSerializers e SerializerConfig.UseDefaultSerializers, quindi esamina il costruttore di SystemTextJsonSerializer indicato nella stack trace. Riproduci l'esempio net48 con System.Text.Json.dll rimosso e traccia quando viene istanziato il serializzatore predefinito. Il lavoro è completato quando un serializzatore Newtonsoft.Json configurato esplicitamente può costruire RestClient senza richiedere System.Text.Json.dll, mentre il serializzatore XML predefinito continua a funzionare.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
RestClient creates its default SystemTextJsonSerializer before applying the caller's configureSerialization callback.
This happens even when the application explicitly configures RestSharp.Serializers.NewtonsoftJson and does not use System.Text.Json for RestSharp serialization or deserialization.
UseDefaultSerializers() configures both SystemTextJsonSerializer and XmlRestSerializer. The XML default is not a problem here. The issue is that the built-in JSON serializer is created before the custom Newtonsoft.Json serializer can replace it.
This requires .NET Framework applications to deploy and load System.Text.Json even when they intentionally use Newtonsoft.Json exclusively.
To Reproduce
- Create a folder and add these two files.
RestSharpNewtonsoftRepro.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RestSharp" Version="114.0.0" />
<PackageReference Include="RestSharp.Serializers.NewtonsoftJson"
Version="114.0.0" />
</ItemGroup>
</Project>
Program.cs:
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;
internal class Program
{
private static void Main()
{
using (var client = new RestClient(
new RestClientOptions("https://example.invalid"),
configureSerialization: serializer =>
serializer.UseNewtonsoftJson()))
{
}
}
}
- Build the project:
dotnet build
- Remove this file from the application output directory:
bin\Debug\net48\System.Text.Json.dll
- Run the generated executable.
Actual behavior
The application fails while constructing RestClient, before making a request or using JSON serialization.
RestSharp creates SystemTextJsonSerializer and its System.Text.Json.JsonSerializerOptions before the Newtonsoft.Json callback is applied. Therefore, System.Text.Json is required even though the configured serializer is Newtonsoft.Json.
Expected behavior
When the caller explicitly configures a JSON serializer, RestSharp should not instantiate the built-in SystemTextJsonSerializer.
For this example, the following should construct RestClient without requiring System.Text.Json.dll:
new RestClient(
new RestClientOptions("https://example.invalid"),
configureSerialization: serializer => serializer.UseNewtonsoftJson());
The default XML serializer may remain configured.
An alternative explicit API to disable the default JSON serializer would also solve the problem. The important behavior is that a custom JSON serializer must be able to replace the default before SystemTextJsonSerializer is created.
Stack trace
System.IO.FileNotFoundException: Could not load file or assembly
'System.Text.Json, Version=10.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.
at RestSharp.Serializers.Json.SystemTextJsonSerializer..ctor()
at RestSharp.Serializers.SerializerConfig.UseDefaultSerializers()
at RestSharp.RestClient.ConfigureSerializers(...)
at RestSharp.RestClient..ctor(...)
at Program.Main()
Desktop
- OS: Windows 11
- .NET version: .NET Framework 4.8
- RestSharp version: 114.0.0
- RestSharp.Serializers.NewtonsoftJson version: 114.0.0
Additional context
This is particularly problematic for plugins loaded into a shared .NET Framework AppDomain. The host or another plugin can load a different strong-named System.Text.Json version first.
An AssemblyResolve handler is not a reliable solution: it is invoked only for failed binds and cannot override an already satisfied host binding or binding redirect.
The issue started when upgrading from RestSharp 112.1.0 to 114.0.0. RestSharp 113 upgraded System.Text.Json to version 10 for all target frameworks.
- Lingua principale
- C#
- Stelle
- 9.8k
- Fork
- 2.3k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di restsharp/RestSharp
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
delete 940 Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 35/100
-
js/calculator.js Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
-
index.html Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 15/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 1/100
Tutte le issue di restsharp/RestSharp
Issue simili
-
bug frontend good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
NavigationViewItemAutomationPeer implements IInvokeProvider but never advertises the Invoke pattern Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
unoplatform/uno#24629 ·
-
agentic-workflows Needs: Triage :mag: State: In-PR
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
CS0162 "Unreachable code detected" warning from a MSBuildTemp .tmp file in every game project Apertabug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
-
Type: enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
apache/arrow-adbc#4809 ·