-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
[dotnet] Fully annotate Command
for AOT support
#15527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
b142f52
8276506
5c6b6ab
bd8ffb3
3f66a71
cebf1d2
0742e3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
using OpenQA.Selenium.Internal; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
|
@@ -31,24 +32,40 @@ namespace OpenQA.Selenium; | |
/// </summary> | ||
public class Command | ||
{ | ||
private readonly static JsonSerializerOptions s_jsonSerializerOptions = new() | ||
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = $"All trimming-unsafe access points to {nameof(s_jsonSerializerOptions)} are annotated as such")] | ||
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = $"All AOT-unsafe access points to {nameof(s_jsonSerializerOptions)} are annotated as such")] | ||
private static class JsonOptionsHolder | ||
{ | ||
TypeInfoResolverChain = | ||
public readonly static JsonSerializerOptions s_jsonSerializerOptions = new() | ||
{ | ||
CommandJsonSerializerContext.Default, | ||
new DefaultJsonTypeInfoResolver() | ||
}, | ||
Converters = { new ResponseValueJsonConverter() } | ||
}; | ||
TypeInfoResolver = GetTypeInfoResolver(), | ||
Converters = { new ResponseValueJsonConverter() } | ||
}; | ||
|
||
private static IJsonTypeInfoResolver GetTypeInfoResolver() | ||
{ | ||
#if NET8_0_OR_GREATER | ||
if (!System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported) | ||
{ | ||
return CommandJsonSerializerContext.Default; | ||
} | ||
#endif | ||
return JsonTypeInfoResolver.Combine(CommandJsonSerializerContext.Default, new DefaultJsonTypeInfoResolver()); | ||
} | ||
} | ||
|
||
private readonly Dictionary<string, object?> _parameters; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Command"/> class using a command name and a JSON-encoded string for the parameters. | ||
/// </summary> | ||
/// <param name="name">Name of the command</param> | ||
/// <param name="jsonParameters">Parameters for the command as a JSON-encoded string.</param> | ||
public Command(string name, string jsonParameters) | ||
: this(null, name, ConvertParametersFromJson(jsonParameters)) | ||
{ | ||
this.SessionId = null; | ||
this._parameters = ConvertParametersFromJson(jsonParameters) ?? new Dictionary<string, object?>(); | ||
this.Name = name ?? throw new ArgumentNullException(nameof(name)); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -61,7 +78,7 @@ public Command(string name, string jsonParameters) | |
public Command(SessionId? sessionId, string name, Dictionary<string, object?>? parameters) | ||
{ | ||
this.SessionId = sessionId; | ||
this.Parameters = parameters ?? new Dictionary<string, object?>(); | ||
this._parameters = parameters ?? new Dictionary<string, object?>(); | ||
this.Name = name ?? throw new ArgumentNullException(nameof(name)); | ||
} | ||
|
||
|
@@ -81,18 +98,32 @@ public Command(SessionId? sessionId, string name, Dictionary<string, object?>? p | |
/// Gets the parameters of the command | ||
/// </summary> | ||
[JsonPropertyName("parameters")] | ||
public Dictionary<string, object?> Parameters { get; } | ||
public Dictionary<string, object?> Parameters | ||
{ | ||
[RequiresUnreferencedCode("Adding untyped parameter values for JSON serialization has best-effort AOT support. Ensure only Selenium types and well-known .NET types are added.")] | ||
[RequiresDynamicCode("Adding untyped parameter values for JSON serialization has best-effort AOT support. Ensure only Selenium types and well-known .NET types are added.")] | ||
get => _parameters; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the parameters of the command as a JSON-encoded string. | ||
/// </summary> | ||
public string ParametersAsJsonString | ||
{ | ||
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = $"All trimming-unsafe access points to {nameof(_parameters)} are annotated as such")] | ||
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = $"All AOT-unsafe access points to {nameof(_parameters)} are annotated as such")] | ||
get | ||
{ | ||
if (this.Parameters != null && this.Parameters.Count > 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (HasParameters()) | ||
{ | ||
return JsonSerializer.Serialize(this.Parameters, s_jsonSerializerOptions); | ||
try | ||
{ | ||
return JsonSerializer.Serialize(this._parameters, JsonOptionsHolder.s_jsonSerializerOptions); | ||
} | ||
catch (NotSupportedException ex) | ||
{ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we support any type, falling back to reflection based if not in the json context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not if we are AOT-compiled. the reflection fallback will not work. That is the only case this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we are not AOT-compatible. Users will don't see warnings at compile time, but will face issues at runtime. And what we?.. just suppress it. It is bad :( I don't see good solution as a quick win, right solution would be avoid any |
||
} | ||
} | ||
else | ||
{ | ||
|
@@ -101,6 +132,25 @@ public string ParametersAsJsonString | |
} | ||
} | ||
|
||
internal bool HasParameters() | ||
{ | ||
return this._parameters != null && this._parameters.Count > 0; | ||
} | ||
|
||
internal bool TryGetValueAndRemoveIfNotNull(string key, [NotNullWhen(true)] out object? value) | ||
{ | ||
if (this._parameters.TryGetValue(key, out value)) | ||
{ | ||
if (value is not null) | ||
{ | ||
this._parameters.Remove(key); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a string of the Command object | ||
/// </summary> | ||
|
@@ -168,5 +218,6 @@ public override string ToString() | |
[JsonSerializable(typeof(Dictionary<string, short>))] | ||
[JsonSerializable(typeof(Dictionary<string, ushort>))] | ||
[JsonSerializable(typeof(Dictionary<string, string>))] | ||
[JsonSerializable(typeof(object[]))] | ||
[JsonSourceGenerationOptions(Converters = [typeof(ResponseValueJsonConverter)])] | ||
internal partial class CommandJsonSerializerContext : JsonSerializerContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,17 +119,13 @@ private static string GetCommandPropertyValue(string propertyName, Command comma | |
propertyValue = commandToExecute.SessionId.ToString(); | ||
} | ||
} | ||
else if (commandToExecute.Parameters != null && commandToExecute.Parameters.Count > 0) | ||
else if (commandToExecute.HasParameters()) | ||
{ | ||
// Extract the URL parameter, and remove it from the parameters dictionary | ||
// so it doesn't get transmitted as a JSON parameter. | ||
if (commandToExecute.Parameters.TryGetValue(propertyName, out var propertyValueObject)) | ||
if (commandToExecute.TryGetValueAndRemoveIfNotNull(propertyName, out var propertyValueObject)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, interesting situation. Transmitter checks each time whether some "infected" parameter should be removed. What are these cases? I hope we can just fix it on consumer side. Based on comments, somebody tries to send There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The case is for parameters in the URL. This extracts those values from the dictionary and uses them in the URL instead of the parameter body. |
||
{ | ||
if (propertyValueObject != null) | ||
{ | ||
propertyValue = propertyValueObject.ToString()!; | ||
commandToExecute.Parameters.Remove(propertyName); | ||
} | ||
propertyValue = propertyValueObject.ToString()!; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ public class WebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor, IFinds | |
/// </summary> | ||
protected static readonly TimeSpan DefaultCommandTimeout = TimeSpan.FromSeconds(60); | ||
private IFileDetector fileDetector = new DefaultFileDetector(); | ||
private NetworkManager network; | ||
private NetworkManager? network; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unintentional change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s intentional and correct (it is lazy-initialized) but not necessarily relevant to this PR. Just one more internal warning gone. |
||
private WebElementFactory elementFactory; | ||
|
||
private readonly List<string> registeredCommands = new List<string>(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to introduce dummy class? Just to hold some object used only in here? I think standard
field
is good.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fields cannot be AOT-annotated, only types and methods. Having a “holder” type is common when you want to annotate specific fields as AOT-unsafe but not everything in the type.