Skip to content

Commit 8276506

Browse files
committed
Avoid silent failures in AOT when type is missing
1 parent b142f52 commit 8276506

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

dotnet/src/webdriver/Command.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ private static class JsonOptionsHolder
3838
{
3939
public readonly static JsonSerializerOptions JsonSerializerOptions = new()
4040
{
41-
TypeInfoResolverChain =
42-
{
43-
CommandJsonSerializerContext.Default,
44-
new DefaultJsonTypeInfoResolver()
45-
},
41+
TypeInfoResolver = GetTypeInfoResolver(),
4642
Converters = { new ResponseValueJsonConverter() }
4743
};
44+
45+
private static IJsonTypeInfoResolver GetTypeInfoResolver()
46+
{
47+
#if NET8_0_OR_GREATER
48+
if (!System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported)
49+
{
50+
return CommandJsonSerializerContext.Default;
51+
}
52+
#endif
53+
return JsonTypeInfoResolver.Combine(CommandJsonSerializerContext.Default, new DefaultJsonTypeInfoResolver());
54+
}
4855
}
4956

5057
private readonly Dictionary<string, object?> _parameters;
@@ -68,8 +75,6 @@ public Command(string name, string jsonParameters)
6875
/// <param name="name">Name of the command</param>
6976
/// <param name="parameters">Parameters for that command</param>
7077
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
71-
[RequiresUnreferencedCode("Adding untyped parameter values for JSON serialization has best-effort AOT support. Ensure only Selenium types and well-known .NET types are added, or use the overload that takes pre-serialized string jsonParameters for guaranteed AOT compatibility.")]
72-
[RequiresDynamicCode("Adding untyped parameter values for JSON serialization has best-effort AOT support. Ensure only Selenium types and well-known .NET types are added, or use the overload that takes pre-serialized string jsonParameters for guaranteed AOT compatibility.")]
7378
public Command(SessionId? sessionId, string name, Dictionary<string, object?>? parameters)
7479
{
7580
this.SessionId = sessionId;
@@ -111,7 +116,14 @@ public string ParametersAsJsonString
111116
{
112117
if (HasParameters())
113118
{
114-
return JsonSerializer.Serialize(this._parameters, JsonOptionsHolder.JsonSerializerOptions);
119+
try
120+
{
121+
return JsonSerializer.Serialize(this._parameters, JsonOptionsHolder.JsonSerializerOptions);
122+
}
123+
catch (NotSupportedException ex)
124+
{
125+
throw new WebDriverException("Attempted to serialize an unsupported type. Ensure you are using Selenium types, or well-known .NET types such as Dictionary<string, object> and object[]", ex);
126+
}
115127
}
116128
else
117129
{
@@ -206,6 +218,7 @@ public override string ToString()
206218
[JsonSerializable(typeof(Dictionary<string, short>))]
207219
[JsonSerializable(typeof(Dictionary<string, ushort>))]
208220
[JsonSerializable(typeof(Dictionary<string, string>))]
221+
[JsonSerializable(typeof(object[]))]
209222
[JsonSourceGenerationOptions(Converters = [typeof(ResponseValueJsonConverter)])]
210223
internal partial class CommandJsonSerializerContext : JsonSerializerContext;
211224
}

dotnet/src/webdriver/WebDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class WebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor, IFinds
4040
/// </summary>
4141
protected static readonly TimeSpan DefaultCommandTimeout = TimeSpan.FromSeconds(60);
4242
private IFileDetector fileDetector = new DefaultFileDetector();
43-
private NetworkManager network;
43+
private NetworkManager? network;
4444
private WebElementFactory elementFactory;
4545

4646
private readonly List<string> registeredCommands = new List<string>();

0 commit comments

Comments
 (0)