From 2383fac0e7df7f9fa99721bad50f828d283effba Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 24 Jan 2025 12:22:57 -0500 Subject: [PATCH] [dotnet] Add nullability to `Command` type (#15147) * [dotnet] Add nullability to `Command` type * Simplify initialization logic of `Command.Parameters` --- dotnet/src/webdriver/Command.cs | 49 ++++++++++++--------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 9cb96055cc952..1559fe9690f5a 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,30 @@ 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; - if (parameters != null) - { - this.commandParameters = parameters; - } - - this.commandName = name; + this.SessionId = sessionId; + this.Parameters = parameters ?? new Dictionary(); + 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; } /// /// Gets the parameters of the command as a JSON-encoded string. @@ -101,13 +87,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 +115,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; } }