Skip to content

[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

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions dotnet/src/webdriver/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,24 +32,33 @@ namespace OpenQA.Selenium
/// </summary>
public class Command
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best viewed with whitespace off

{
private readonly static JsonSerializerOptions s_jsonSerializerOptions = new()
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = $"All trimming-unsafe access points to {nameof(JsonSerializerOptions)} are annotated as such")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = $"All AOT-unsafe access points to {nameof(JsonSerializerOptions)} are annotated as such")]
private static class JsonOptionsHolder
{
TypeInfoResolverChain =
public readonly static JsonSerializerOptions JsonSerializerOptions = new()
{
CommandJsonSerializerContext.Default,
new DefaultJsonTypeInfoResolver()
},
Converters = { new ResponseValueJsonConverter() }
};
TypeInfoResolverChain =
{
CommandJsonSerializerContext.Default,
new DefaultJsonTypeInfoResolver()
},
Converters = { new ResponseValueJsonConverter() }
};
}

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>
Expand All @@ -58,10 +68,12 @@ public Command(string name, string jsonParameters)
/// <param name="name">Name of the command</param>
/// <param name="parameters">Parameters for that command</param>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
[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.")]
[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.")]
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));
}

Expand All @@ -81,18 +93,25 @@ 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)
if (HasParameters())
{
return JsonSerializer.Serialize(this.Parameters, s_jsonSerializerOptions);
return JsonSerializer.Serialize(this._parameters, JsonOptionsHolder.JsonSerializerOptions);
}
else
{
Expand All @@ -101,6 +120,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>
Expand Down
10 changes: 3 additions & 7 deletions dotnet/src/webdriver/HttpCommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,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))
{
if (propertyValueObject != null)
{
propertyValue = propertyValueObject.ToString()!;
commandToExecute.Parameters.Remove(propertyName);
}
propertyValue = propertyValueObject.ToString()!;
}
}

Expand Down
Loading