From 54bdcb495f9372f5e04322c60fbd7a976f6e7e30 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 24 Jan 2025 11:51:09 -0500 Subject: [PATCH 1/2] [dotnet] Add nullability to `Command` type --- dotnet/src/webdriver/Command.cs | 45 +++++++++++++-------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 9cb96055cc952..cdc27313e7c41 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -18,11 +18,14 @@ // using OpenQA.Selenium.Internal; +using System; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; +#nullable enable + namespace OpenQA.Selenium { /// @@ -30,10 +33,6 @@ namespace OpenQA.Selenium /// public class Command { - private SessionId commandSessionId; - private string commandName; - private Dictionary commandParameters = new Dictionary(); - private readonly static JsonSerializerOptions s_jsonSerializerOptions = new() { TypeInfoResolver = JsonTypeInfoResolver.Combine(CommandJsonSerializerContext.Default, new DefaultJsonTypeInfoResolver()), @@ -56,43 +55,34 @@ public Command(string name, string jsonParameters) /// Session ID the driver is using /// Name of the command /// Parameters for that command - public Command(SessionId sessionId, string name, Dictionary parameters) + public Command(SessionId? sessionId, string name, Dictionary? parameters) { - this.commandSessionId = sessionId; + this.SessionId = sessionId; if (parameters != null) { - this.commandParameters = parameters; + this.Parameters = parameters; } - this.commandName = name; + this.Name = name; } /// /// Gets the SessionID of the command /// [JsonPropertyName("sessionId")] - public SessionId SessionId - { - get { return this.commandSessionId; } - } + public SessionId? SessionId { get; } /// /// Gets the command name /// [JsonPropertyName("name")] - public string Name - { - get { return this.commandName; } - } + public string Name { get; } /// /// Gets the parameters of the command /// [JsonPropertyName("parameters")] - public Dictionary Parameters - { - get { return this.commandParameters; } - } + public Dictionary Parameters { get; } = new Dictionary(); /// /// Gets the parameters of the command as a JSON-encoded string. @@ -101,13 +91,12 @@ public string ParametersAsJsonString { get { - string parametersString = string.Empty; - if (this.commandParameters != null && this.commandParameters.Count > 0) + string parametersString; + if (this.Parameters != null && this.Parameters.Count > 0) { - parametersString = JsonSerializer.Serialize(this.commandParameters, s_jsonSerializerOptions); + parametersString = JsonSerializer.Serialize(this.Parameters, s_jsonSerializerOptions); } - - if (string.IsNullOrEmpty(parametersString)) + else { parametersString = "{}"; } @@ -130,9 +119,11 @@ public override string ToString() /// /// The JSON-encoded string representing the command parameters. /// A with a string keys, and an object value. - private static Dictionary ConvertParametersFromJson(string value) + /// If is not a JSON object. + /// If is . + private static Dictionary? ConvertParametersFromJson(string value) { - Dictionary parameters = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions); + Dictionary? parameters = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions); return parameters; } } From e7e4155a50c22e5944a98627211a96de688528d2 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 24 Jan 2025 11:54:25 -0500 Subject: [PATCH 2/2] Simplify initialization logic of `Command.Parameters` --- dotnet/src/webdriver/Command.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index cdc27313e7c41..1559fe9690f5a 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -58,11 +58,7 @@ public Command(string name, string jsonParameters) public Command(SessionId? sessionId, string name, Dictionary? parameters) { this.SessionId = sessionId; - if (parameters != null) - { - this.Parameters = parameters; - } - + this.Parameters = parameters ?? new Dictionary(); this.Name = name; } @@ -82,7 +78,7 @@ public Command(SessionId? sessionId, string name, Dictionary? pa /// Gets the parameters of the command /// [JsonPropertyName("parameters")] - public Dictionary Parameters { get; } = new Dictionary(); + public Dictionary Parameters { get; } /// /// Gets the parameters of the command as a JSON-encoded string.